Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

SAS - Split single column into two based on value of an ID column

I have data which is as follows.

data have;
  input group replicate $ sex $ count;

datalines;
1 A F 3
1 A M 2
1 B F 4
1 B M 2
1 C F 4
1 C M 5
2 A F 5
2 A M 4
2 B F 6
2 B M 3
2 C F 2
2 C M 2
3 A F 5
3 A M 1
3 B F 3
3 B M 4
3 C F 3
3 C M 1
;
run;

I want to break the count column into two separate columns based on gender.

                                                 count_    count_
                    Obs    group    replicate    female     male

                     1       1          A           3         2
                     2       1          B           4         2
                     3       1          C           4         5
                     4       2          A           5         4
                     5       2          B           6         3
                     6       2          C           2         2
                     7       3          A           5         1
                     8       3          B           3         4
                     9       3          C           3         1

This can be done by first creating two separate data sets for each level of sex and then performing a merge.

data just_female;
  set have;
  where sex = 'F';
  rename count = count_female;
run;

data just_male;
  set have;
  where sex = 'M';
  rename count = count_male;
run;

data want;
  merge
    just_female
    just_male
  ;
  by
    group
    replicate
  ;

  keep
    group
    replicate
    count_female
    count_male
  ;
run;

Is there a less verbose way to do this which doesn't require the need to sort or explicitly drop/keep variables?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this using proc transpose but you will need to sort the data. I believe this is what you're looking for though.

proc sort data=have;
by group replicate;
run;

The data is sorted so now you have your by-group for transposing.

proc transpose data=have out=want(drop=_name_) prefix=count_;
by group replicate;
id sex;
var count;
run;

proc print data=want;

Then you get:

Obs    group    replicate    count_F    count_M

 1       1          A           3          2
 2       1          B           4          2
 3       1          C           4          5
 4       2          A           5          4
 5       2          B           6          3
 6       2          C           2          2
 7       3          A           5          1
 8       3          B           3          4
 9       3          C           3          1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...