楼主: cauhellen
3855 6

[有偿编程] 求两个数据行合并成一行,5个论坛币答谢! [推广有奖]

  • 0关注
  • 0粉丝

高中生

62%

还不是VIP/贵宾

-

威望
0
论坛币
176 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
403 点
帖子
21
精华
0
在线时间
37 小时
注册时间
2007-6-22
最后登录
2016-6-3

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; ...
关键词:5个论坛币 行合并 论坛币 subtotal Beijing 演艺圈 香港 赌圣 拍摄
沙发
alfine 发表于 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

已有 1 人评分论坛币 学术水平 热心指数 收起 理由
Imasasor + 60 + 1 + 1 热心帮助其他会员

总评分: 论坛币 + 60  学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

藤椅
webgu 发表于 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;
SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

使用道具

板凳
zhengbo8 发表于 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


使用道具

报纸
webgu 发表于 2013-1-18 12:38:28 |只看作者 |坛友微信交流群
alfine 发表于 2013-1-18 11:44
data test;
input city :$20.  Q1  Q2;
datalines;
嗯,LAG有些投机取巧了。
SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

使用道具

地板
cauhellen 发表于 2013-1-18 14:28:55 |只看作者 |坛友微信交流群
谢谢大家的答复!

使用道具

7
ihust 发表于 2013-1-18 22:42:56 |只看作者 |坛友微信交流群
/*  第一个问题 */
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;
data tmp;
        input city $2. Q1 Q2;
datalines;
ZJ 42 36
BJ 90 50
;
run;
proc sort data=test;by city;run;
proc sort data=tmp;by city;run;
data test;
        merge test tmp;
        by city;
        if city = '' then delete;
run;
proc print data=test;run;

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 22:20