Hello
I want to stack multiple data sets using SET statement.
The data sets are: t2001,t2004,t2005 (Please note that last 4 digits are YYMM form).
I want to create a column with information from which data set the observation came from.
If observation came from data set t2001 then period=2001
If observation came from data set t2004 then period=2004
If observation came from data set t2005 then period=2005
What is the way to do it please?
Data t2001;
input ID y
cards
1 10
2 20
;
run;
Data t2004;
input ID y
cards
1 10
3 40
;
run;
Data t2005;
input ID y
cards
1 15
2 25
3 50
;
run;
Data t_All;
SET t2001 t2004 t2005;
IF observation from data set t2001 then period=2001;
IF observation from data set t2004 then period=2004;
IF observation from data set t2005 then period=2005;
Run;
Hi @Ronein Good afternoon, What you need is to use INDSNAME= option
Data t2001;
input ID y;
cards;
1 10
2 20
;
run;
Data t2004;
input ID y;
cards;
1 10
3 40
;
run;
Data t2005;
input ID y;
cards;
1 15
2 25
3 50
;
run;
Data t_All;
SET t2001 t2004 t2005 indsname=t;
period=compress(t,,'kd');
Run;
data t_all;
set t2001 (in=in1) t2004 (in=in2) t2005 (in=in3);
if in1 then period = 2001;
else if in2 then period = 2004;
else if in3 then period = 2005;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.