Hi SAS masters,
I have two datasets below. FirmID and Year are the same.
Have1
FirmID Year Variable1
1001 1995 AA
1001 1996 BB
1001 1997 CC
1002 1992 DD
1002 1993 EE
1002 1994 FF
Have2
FirmID Year Variable2
1001 1995 GG
1001 1996 HH
1001 1997 II
1002 1992 JJ
1002 1993 KK
1002 1994 LL
I would like to add variable1 as observations to variable2 and expect the data like below.
FirmID Year Variable1_2
what code do I need to use? Thanks.
Just rename it on the way in.
data want ; set have1(rename=(variable1=variable1_2)) have2(rename=(variable2=variable1_2)) ; run;
If the data was already sorted by FIRMID YEAR you can add a BY statement and the result will still be sorted.
data want ; set have1(rename=(variable1=variable1_2)) have2(rename=(variable2=variable1_2)) ; by firmid year ; run;
View solution in original post
proc sql; create table want as select FirmID, Year, Variable1 as Variable1_2 from (select * from have1 union all select * from have2) order by firmid,year; quit;
data have1; input FirmID Year Variable1 $; datalines; 1001 1995 AA 1001 1996 BB 1001 1997 CC 1002 1992 DD 1002 1993 EE 1002 1994 FF ; data have2; input FirmID Year Variable2 $; datalines; 1001 1995 GG 1001 1996 HH 1001 1997 II 1002 1992 JJ 1002 1993 KK 1002 1994 LL ; data want; set have2 (rename=Variable2=Variable1_2); output; set have1 (rename=Variable1=Variable1_2); output; run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.