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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 704 views
  • 2 likes
  • 2 in conversation