How do i create a new date variable using other date variables?

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

How do i create a new date variable using other date variables?

[ Edited ]

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!


Accepted Solutions
Solution
a month ago
Super User
Posts: 23,758

Re: How do i create a new date variable using other date variables?

Data Final;

Set Data1 (rename=date1=date_final)

       Data2 (rename=date2=date_final)

      Data3 ....Data10;

by Uniq_ID;

run;

View solution in original post


All Replies
Solution
a month ago
Super User
Posts: 23,758

Re: How do i create a new date variable using other date variables?

Data Final;

Set Data1 (rename=date1=date_final)

       Data2 (rename=date2=date_final)

      Data3 ....Data10;

by Uniq_ID;

run;

Super User
Posts: 13,574

Re: How do i create a new date variable using other date variables?

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.

Super User
Posts: 23,758

Re: How do i create a new date variable using other date variables?


@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.

Contributor
Posts: 22

Re: How do i create a new date variable using other date variables?

Ok I will not use the by statement.

PROC Star
Posts: 1,822

Re: How do i create a new date variable using other date variables?

[ Edited ]

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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