SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
qatman28
Obsidian | Level 7
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;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2 replies
  • 812 views
  • 2 likes
  • 3 in conversation