全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4099 6
2013-01-18
悬赏 5 个论坛币 已解决
data test;
input city$2.  Q1  Q2;
datalines;
ZJ . .        
HZ    10    11
NB    20    12
LS    12    13
    42    36
BJ . .        
HD    20    14
CW    30    15
CP    40    21
    90    50
;
run;
以上是我的数据, 我需要把subtotal(就是city那列没有变量名所在的行)这行加到ZJ 和BJ这行去,希望得到的结果是这样:谢谢!
ZJ

42

36

HZ

10

11

NB

20

12

LS

12

13

BJ

90

50

HD

20

14

CW

30

15

CP

40

21



另外,在输入数据的时候,如果zhejiang 和beijing 比city变量下的其他名字长,要如何设置input 后面变量的格式,谢谢!
data test;
input city$2.  Q1  Q2;
datalines;
Zhejiang . .        
HZ    10    11
NB    20    12
LS    12    13
    42    36
Beijing . .        
HD    20    14
CW    30    15
CP    40    21
    90    50
;
run;



最佳答案

alfine 查看完整内容

data test; input city :$20. Q1 Q2; datalines; Zhejiang . . HZ 10 11 NB 20 12 LS 12 13 . 42 36 Beijing . . HD 20 14 CW 30 15 CP 40 21 . 90 50 ; run; data temp; set test; retain cityx; if Q1=. then cityx=city; run; data want(drop=cityx); set temp; if q1=. then delete; if city="" then city=cityx; ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-1-18 03:10:51
data test;
input city :$20.  Q1  Q2;
datalines;
Zhejiang . .        
HZ    10    11
NB    20    12
LS    12    13
.    42    36
Beijing . .        
HD    20    14
CW    30    15
CP    40    21
.   90    50
;
run;

data temp;
set test;
retain cityx;
if Q1=. then cityx=city;
run;
data want(drop=cityx);
set temp;
if q1=. then delete;
if city="" then city=cityx;
run;
优点是可以自动识别beijing等地名,地名下面数据部分的行数可以不固定
运行结果:
QQ截图20130118113603.png
附件列表
QQ截图20130118113603.png

原图尺寸 3.25 KB

QQ截图20130118113603.png

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-18 09:21:39
data test;
input city :$20.  Q1  Q2;
datalines;
Zhejiang . .        
HZ    10    11
NB    20    12
LS    12    13
.    42    36
Beijing . .        
HD    20    14
CW    30    15
CP    40    21
.   90    50
;
run;

data want(rename=(city_f=city)  drop=city);
  set test;
city_f=lag4(city);
if not  missing(city)  then city_f=city;
if  missing(q1) and missing(Q2) then delete;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-18 10:55:59
我想到的是数据集合并的方法
data.txt中原始数据:
ZheJiang . .        
HZ    10    11
NB    20    12
LS    12    13
    42    36
BeiJing . .        
HD    20    14
CW    30    15
CP    40    21
    90    50

SAS代码:
data subtotal;
        infile 'D:\data.txt' ;
        input #1 city $ #5 Q1 Q2;output;
        input #6 city $ #10 Q1 Q2; output;
run;

data test1;
        infile 'D:\data.txt' truncover;
        input city $  Q1  Q2;
        if _N_ not in (5,10);
run;

data test;
        set test1;
        m=1;
        if _N_=1 then set subtotal point=m;
        n=2;
        if _N_=5 then set subtotal point=n;
run;


运行结果:

ZheJiang

42

36

HZ

10

11

NB

20

12

LS

12

13

BeiJing

90

50

HD

20

14

CW

30

15

CP

40

21


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-18 12:38:28
alfine 发表于 2013-1-18 11:44
data test;
input city :$20.  Q1  Q2;
datalines;
嗯,LAG有些投机取巧了。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-18 14:28:55
谢谢大家的答复!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群