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

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          

1001       1995               GG   

1001       1995               AA                

1001       1996              HH 

1001       1996              BB                   

1001       1997              II

1001       1997              CC

1002       1992              DD 

1002       1992              JJ 

1002       1993              EE

1002       1993              KK

1002       1994              FF   

1002       1994              LL   

 

what code do I need to use? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
novinosrin
Tourmaline | Level 20
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;
PeterClemmensen
Tourmaline | Level 20
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;
Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 370 views
  • 0 likes
  • 4 in conversation