BookmarkSubscribeRSS Feed
HEB1
Calcite | Level 5

 

Hi 

my data looks like this: 

data have; input id $ year variable1 variable2 variable3 variable4 variable5 variable6 variable7

x 1999 0 1 0 1 0 0
x 2005 1 1 1 1 1 1
x 2006 0 1 0 1 0 0
y 1989 1 1 1 1 1 1
y 2010 0 1 0 1 0 0

I want to condense all IDs to 1 row so my data will look like this:

id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
x 1999 0 1 0 1 0 0 2005 1 1 1 1 1 1 2006 0 1 0 1 0 0
y 1989 1 1 1 1 1 1 2010 0 1 0 1 0 0
z 2000 1 1 1 1 1 1 2003 0 1 0 1 0 0 2004 1 1 1 1 1 1

Thanks! 

9 REPLIES 9
PaigeMiller
Diamond | Level 26
data have; input id $ year variable1 variable2 variable3 variable4 variable5 variable6 variable7

x 1999 0 1 0 1 0 0
x 2005 1 1 1 1 1 1
x 2006 0 1 0 1 0 0
y 1989 1 1 1 1 1 1
y 2010 0 1 0 1 0 0

 

This is not working SAS code. Could you please fix it (and also please test your code before you provide it). Thanks!

--
Paige Miller
HEB1
Calcite | Level 5

data regd.have; input id $ year $ variable1 $ variable2 $ variable3 $ variable4 $ variable5 $ variable6 $ variable7;datalines;

x 1999 0 1 0 1 0 0 0
x 2005 1 1 1 1 1 1 0
x 2006 0 1 0 1 0 0 0
y 1989 1 1 1 1 1 1 0
y 2010 0 1 0 1 0 0 0
;

 

here is the correct code for the data 

HEB1
Calcite | Level 5
data regd.have; input id $ year $ variable1 $ variable2 $ variable3 $ variable4 $ variable5 $ variable6 $ variable7;datalines;

x 1999 0 1 0 1 0 0 0
x 2005 1 1 1 1 1 1 0
x 2006 0 1 0 1 0 0 0
y 1989 1 1 1 1 1 1 0
y 2010 0 1 0 1 0 0 0
;

I want to condense all IDs to 1 row so my data will look like this:

id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
x 1999 0 1 0 1 0 0 2005 1 1 1 1 1 1 2006 0 1 0 1 0 0
y 1989 1 1 1 1 1 1 2010 0 1 0 1 0 0
z 2000 1 1 1 1 1 1 2003 0 1 0 1 0 0 2004 1 1 1 1 1 1

Ksharp
Super User
data have; 
input id $ year $ variable1 $ variable2 $ variable3 $ variable4 $ variable5 $ variable6 $ variable7;
datalines;
x 1999 0 1 0 1 0 0 0
x 2005 1 1 1 1 1 1 0
x 2006 0 1 0 1 0 0 0
y 1989 1 1 1 1 1 1 0
y 2010 0 1 0 1 0 0 0
;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql noprint;
select distinct catt('temp(where=(n=',n,') rename=(year=year',n,' variable1-variable7=variable',n,'_1-variable',n,'_7))')
 into : merge separated by ' ' 
 from temp;
quit;
data want;
 merge &merge.;
 by id;
 drop n;
run;
HEB1
Calcite | Level 5
Hi,
I don't understand this row:
rename=(year=year',n,' variable1-variable7=variable',n,'_1-variable',n,'_7))')
do I need to write over there the right names of each of the variables?
HEB1
Calcite | Level 5

Hi, I have this data

 

data regd.have; input id $ year $ FD $ REPORTING $ variable3 $ variable4 $ variable5 $ variable6 $ variable7;datalines;

x 1999 0 1 0 1 0 0 0
x 2005 1 1 1 1 1 1 0
x 2006 0 1 0 1 0 0 0
y 1989 1 1 1 1 1 1 0
y 2010 0 1 0 1 0 0 0
;

I want to condense all IDs to 1 row so my data will look like this:, Meaning i want to copy each row to the row above - if it has same cik. 

id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
id year variable 2 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 7 variable 3 variable 4 variable 5 variable 6 variable 8 year variable 2 variable 3 variable 4 variable 5 variable 6 variable 7
x 1999 0 1 0 1 0 0 2005 1 1 1 1 1 1 2006 0 1 0 1 0 0
y 1989 1 1 1 1 1 1 2010 0 1 0 1 0 0
z 2000 1 1 1 1 1 1 2003 0 1 0 1 0 0 2004 1 1 1 1 1 1

Ksharp
Super User
Yes. you need to write right variable names if your variable names are different with variable1 variable2.....

My macro variable contains the following code :

temp(where=(n=1) rename=(year=year1 variable1-variable7=variable1_1-variable1_7)) 
temp(where=(n=2) rename=(year=year2 variable1-variable7=variable2_1-variable2_7)) 
temp(where=(n=3) rename=(year=year3 variable1-variable7=variable3_1-variable3_7)) 
Kurt_Bremser
Super User

dataset structure like this is not possible, as you cannot have multiple variables with the same name.

Also, such a wide structure is VERY hard to handle in future code; it only makes sense if this is to be a report for human consumption.

So, shall this be a report?

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 9 replies
  • 1194 views
  • 0 likes
  • 4 in conversation