BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi Experts,

 

I want to merge my data in the below-given format. Kindly help me solve it.

 

 

data aaa;
input ID date1 mmddyy10. reg$;
format date1 date9.;
cards;
100 02/10/1990 V
100 05/09/1991 V
100 12/03/2000 V
101 05/05/2000 V
101 08/07/2010 V
101 04/03/2015 V
;

data bbb;
input ID date1 mmddyy10. reg$;
format date1 date9.;
cards;
100 02/10/1990 R
100 07/08/1995 R
100 12/03/2000 R
101 01/01/2001 R
101 08/07/2010 R
101 04/03/2015 R
;

Required Output : 

IDdate1reg
10010-02-1990V
10009-05-1991V
10008-07-1995R
10003-12-2000V
10105-05-2000V
10101-01-2001R
10107-08-2010V
10103-04-2015V

 


V - Visit; R - Record 

If visit_date same as record_date  then reg should 'V'

If record_date not in aaa table then that line should be included in the Current table/New Table with reg value of 'R'.

 

Please let me know If you required clarification.

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

data ab;
	merge aaa(rename=(reg=reg_a) in=in1) bbb(rename=(reg=reg_b) in=in2);
	by id date1;
	if reg_a='V' then reg='V';
	else if reg_b='R' and not in1 then reg='R';
run;

In this small example, every case is handled by the two IF statements. Are there other possible cases?

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

data ab;
	merge aaa(rename=(reg=reg_a) in=in1) bbb(rename=(reg=reg_b) in=in2);
	by id date1;
	if reg_a='V' then reg='V';
	else if reg_b='R' and not in1 then reg='R';
run;

In this small example, every case is handled by the two IF statements. Are there other possible cases?

--
Paige Miller
Astounding
PROC Star
Given that both of the data sets are already sorted, you can use:

data want;
merge bbb aaa;
by id date1;
run;

You will see a note in the log since REG appears in both data sets. Ignore it.
Sathish_jammy
Lapis Lazuli | Level 10

Thank you for your quick support @Astounding  @PaigeMiller  

Both your code works well. Have a great day!

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 871 views
  • 0 likes
  • 3 in conversation