BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RTN
Calcite | Level 5 RTN
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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

 

novinosrin
Tourmaline | Level 20

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2057 views
  • 0 likes
  • 2 in conversation