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

Hi all,

I am trying to create a new data set by stacking 10 different data sets. Each of these data sets have a uniq_ID and a Date variable (eg. Date1 for data1 and date2 for Data2, date3 for data3, etc.). My goal is to create a new variable called Date_Final using Date1, date2, Date3, Dat4, ....Date10. Essentially my new date variable, Date_Final should copy dates from all 10 data sets and create a new column.

 

Data Final;

Set Data1 Data2 Data3 ....Data10;

by Uniq_ID;

run;

 

Each of these data sets contain the following:

Data1: Uniq_ID and Date1

Data2: Uniq_ID and Date2

Data3: Uniq_ID and Date3

.....

.....

Data10: Uniq_ID and Date10

 

Uniq_ID's can be same or different in each of these data sets, I don't care about them since I am stacking them.

 

Final data set should contain:

Uniq_ID    Date_Final

 

 

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Data Final;

Set Data1 (rename=date1=date_final)

       Data2 (rename=date2=date_final)

      Data3 ....Data10;

by Uniq_ID;

run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Data Final;

Set Data1 (rename=date1=date_final)

       Data2 (rename=date2=date_final)

      Data3 ....Data10;

by Uniq_ID;

run;

ballardw
Super User

if the dates are actually SAS date valued numeric then this should work:

 

date_final= max(of date1 -date10);

if the ONLY values you want at the end are Uniq_ID    Date_Final then use a keep statement:

keep Uniq_ID    Date_Final;

if there are other variables involved but you want to drop the various date variables then use drop:

 

drop date1-date10;

 

 

 

A good idea is not to put "data" into variable names. If you had a  single name, "date" for instance, this step would not be needed when appending data with this method.

Reeza
Super User

@sms1891 wrote:

Uniq_ID's can be same or different in each of these data sets, I don't care about them since I am stacking them.

 

If you're stacking why do you have the BY statement? I suspect that will not give you the results you want.

sms1891
Quartz | Level 8

Ok I will not use the by statement.

novinosrin
Tourmaline | Level 20

You could adopt this approach perhaps:

 

Demo

 

data w1 w2 w3;
set sashelp.class;
run;


data w1;
set w1;
date1=today();
run;

data w2;
set w2;
date2=today()+1;
run;


data w3;
set w3;
date3=today()+2;
run;

data want;
set w1 w2 w3 indsname=k;
k1=k;
date_final=sum(of date: );
run;

 

 

 

or date_final=coalesce(of date:  )  ; 

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
  • 5 replies
  • 1900 views
  • 1 like
  • 4 in conversation