BookmarkSubscribeRSS Feed
righcoastmike
Quartz | Level 8

******REPOST******* I had posted this in the Base SAS programming community, but it was suggested that it might be more helpful over here. Here is the link to the original thread: https://communities.sas.com/t5/Base-SAS-Programming/re-coding-multiple-records-to-match-the-first-in...

 

In short. I had multiple records per individual,  all with the potential to have different postal codes. I needed all of the postal codes to match up to the first postal code recorded for each individual (seq_no = 1).

 

I managed to figure it out and thought I would post the answer just in case anyone has the same issue. 

 

Here's the code: 

 

input

data have;
input patient_id Seq_num post_code $;
cards;1 4 M4F
1 2 M4R
1 3 M4R
1 1 AAA
2 1 BBB
2 3 M4X
2 2 M4T
3 1 CCC
3 2 M4M
4 1 DDD;
run;

proc sort;
by patient_id seq_no;
run;

data work.sorted;
set work.have;
by patient_id;
retain pcfixed;
if first.patient_id then pcfixed=post_code;
run; 

Which produces an output of:

Patient_ID Seq_no Post_code pcfixed
       1      4      M4F       AAA
       1      2      M4R       AAA
       1      3      M4R       AAA
       1      1      AAA       AAA
       2      1      BBB       BBB
       2      3      M4X       BBB
       2      2      M4T       BBB
       3      1      CCC       CCC
       3      2      M4M       CCC
       4      1      DDD       DDD

hopefully this helps someone trying to do a similar thing in the future! 

 

Mike 

1 REPLY 1
Ksharp
Super User
An alternative way.





data have;
input patient_id Seq_num post_code $;
cards;
1 4 M4F
1 2 M4R
1 3 M4R
1 1 AAA
2 1 BBB
2 3 M4X
2 2 M4T
3 1 CCC
3 2 M4M
4 1 DDD
;
run;
data want;
 merge have have(keep=patient_id Seq_num post_code
rename=(Seq_num=_seq post_code=pcfixed ) where=(_seq=1));
by patient_id;
drop _:;
run;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1 reply
  • 280 views
  • 2 likes
  • 2 in conversation