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

dataset 1

IDDate
101/13/2017
101/14/2017
101/15/2017
102/15/2018
201/13/2017
201/15/2017
202/15/2018
301/13/2017
301/15/2017
3

02/15/2018

 

dataset 2

IDlevelDate
1A01/01/1993
1B01/14/2017
1C12/17/2017
2A05/24/2017
3A11/11/2016

 

Want

IDDateLevel
101/13/2017A
101/14/2017B
101/15/2017B
102/15/2018C
201/13/2017 
201/15/2017 
202/15/2018A
301/13/2017A
301/15/2017A
302/15/2018A

 

Hi there.

 

I want to merge the dataset1 and dataset2 by id and date. For each ID, I want to add variable 'level' to dataset1 based on its last record (in dataset2) before the given date in dataset1.

 

For example, for a given date 01/13/2017 in dateset1. I want the level variable to be 'A', since according to dataset2, the last record before 01/13/2017 is 'A' ( on 01/01/1993).

 

Any help to achieve this in sas?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Actually, what you are  trying to do is not a standard merge, and I doubt that reading the documentation will help.  It's mildly complex.  Assuming that both data sets are sorted by ID and DATE:

 

data want;

set  dataset2 (in=in2 rename=(level=fetch_this_level)) dataset1 (in=in1);

by id date;

length level $ 1;    /* might need to be longer ... in real life it might not be just "A" "B" "C" */

retain level;

if first.ID then level = ' ';

if in2 then level = fetch_this_level;

if in1;

drop fetch_this_level;

run;

View solution in original post

4 REPLIES 4
Reeza
Super User

The documentation should be the first place you check. 

 

https://documentation.sas.com/?docsetId=lrcon&docsetTarget=p15jvywi5avt3cn1bee8r6c33ux1.htm&docsetVe...

 


@wsydy wrote:

dataset 1

ID Date
1 01/13/2017
1 01/14/2017
1 01/15/2017
1 02/15/2018
2 01/13/2017
2 01/15/2017
2 02/15/2018
3 01/13/2017
3 01/15/2017
3

02/15/2018

 

dataset 2

ID level Date
1 A 01/01/1993
1 B 01/14/2017
1 C 12/17/2017
2 A 05/24/2017
3 A 11/11/2016

 

Want

ID Date Level
1 01/13/2017 A
1 01/14/2017 B
1 01/15/2017 B
1 02/15/2018 C
2 01/13/2017  
2 01/15/2017  
2 02/15/2018 A
3 01/13/2017 A
3 01/15/2017 A
3 02/15/2018 A

 

Hi there.

 

I want to merge the dataset1 and dataset2 by id and date. For each ID, I want to add variable 'level' to dataset1 based on its last record (in dataset2) before the given date in dataset1.

 

For example, for a given date 01/13/2017 in dateset1. I want the level variable to be 'A', since according to dataset2, the last record before 01/13/2017 is 'A' ( on 01/01/1993).

 

Any help to achieve this in sas?

Thanks


 

Astounding
PROC Star

Actually, what you are  trying to do is not a standard merge, and I doubt that reading the documentation will help.  It's mildly complex.  Assuming that both data sets are sorted by ID and DATE:

 

data want;

set  dataset2 (in=in2 rename=(level=fetch_this_level)) dataset1 (in=in1);

by id date;

length level $ 1;    /* might need to be longer ... in real life it might not be just "A" "B" "C" */

retain level;

if first.ID then level = ' ';

if in2 then level = fetch_this_level;

if in1;

drop fetch_this_level;

run;

wsydy
Calcite | Level 5

Thanks very much for your help!! It works!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the 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
  • 4 replies
  • 2321 views
  • 5 likes
  • 4 in conversation