Hi
I would like help with creating multiple observations from one observation.
My data set looks like this
ID start stop x1 x2 x3 x4
1 2000 2015 1 2 3 4
2 2005 2009 0 2 3 0
3 2000 2002 1 0 0 0
4 2007 2013 0 2 3 4
What I want is an observation per every x=1:
ID start stop x
1 2000 2015 1
1 2000 2015 2
1 2000 2015 3
1 2000 2015 4
2 2005 2009 2
2 2005 2009 3
3 2000 2002 1
4 2007 2013 2
4 2007 2013 3
4 2007 2013 4
How do I do that?
Hi @RTN It's a straight forward transpose-
data have;
input ID start stop x1 x2 x3 x4;
cards;
1 2000 2015 1 2 3 4
2 2005 2009 0 2 3 0
3 2000 2002 1 0 0 0
4 2007 2013 0 2 3 4
;
proc transpose data=have out=want(where=(x) rename=(col1=x) drop=_:);
by id start stop;
var x1-x4;
run;
proc print noobs;run;
ID | start | stop | x |
---|---|---|---|
1 | 2000 | 2015 | 1 |
1 | 2000 | 2015 | 2 |
1 | 2000 | 2015 | 3 |
1 | 2000 | 2015 | 4 |
2 | 2005 | 2009 | 2 |
2 | 2005 | 2009 | 3 |
3 | 2000 | 2002 | 1 |
4 | 2007 | 2013 | 2 |
4 | 2007 | 2013 | 3 |
4 | 2007 | 2013 | 4 |
Hi @RTN It's a straight forward transpose-
data have;
input ID start stop x1 x2 x3 x4;
cards;
1 2000 2015 1 2 3 4
2 2005 2009 0 2 3 0
3 2000 2002 1 0 0 0
4 2007 2013 0 2 3 4
;
proc transpose data=have out=want(where=(x) rename=(col1=x) drop=_:);
by id start stop;
var x1-x4;
run;
proc print noobs;run;
ID | start | stop | x |
---|---|---|---|
1 | 2000 | 2015 | 1 |
1 | 2000 | 2015 | 2 |
1 | 2000 | 2015 | 3 |
1 | 2000 | 2015 | 4 |
2 | 2005 | 2009 | 2 |
2 | 2005 | 2009 | 3 |
3 | 2000 | 2002 | 1 |
4 | 2007 | 2013 | 2 |
4 | 2007 | 2013 | 3 |
4 | 2007 | 2013 | 4 |
And if you prefer transposing using ARRAY-
data have;
input ID start stop x1 x2 x3 x4;
cards;
1 2000 2015 1 2 3 4
2 2005 2009 0 2 3 0
3 2000 2002 1 0 0 0
4 2007 2013 0 2 3 4
;
data want;
set have;
array t x1-x4;
do over t;
x=t;
if x then output;
end;
drop x1-x4;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.