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:  )  ; 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 680 views
  • 1 like
  • 4 in conversation