BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
terjeph
Obsidian | Level 7

I hava dataset indicating where people lived the last years of life. I'd like to select the last variable (the zip code during the death year) for each observation (patientID) and write it to a new file. Please observe patientID 4 which have some missings at the first years.

 

Data have:

PatientID zip2008 zip2009 zip2010 zip2011 zip2012

1                         304         304         304

2                        403

3                        506         506          704

4                                                          809       809      809

 

data want:

PatientID   LastZip

1                    304

2                   403

3                   704

4                   809

 

Anyone?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

COALESCE() or COALESCEC() if the variables are character should do it.

Just use a descending order variable list.

data want;
  set have;
   lastzip=coalescec(of zip2012-zip2008);
run;

View solution in original post

5 REPLIES 5
quickbluefish
Barite | Level 11
data want;
set have;
array z {*} zip:;
length lastZip $5;
do i=1 to dim(z);
    if not missing(z[i]) then lastZip=z[i];
end;
keep patientID lastZip;
run;

Change the length statement as appropriate -- this is currently assuming zip is a character variable of length 5 ($5).  

 

 

PaigeMiller
Diamond | Level 26

Better to arrange your data in the long format.

 

data have;
input patient_id year zip;
cards;
1 2008 304
1 2009 304
1 2010 304
2 2008 403
3 2008 506
3 2009 506
3 2010 704
4 2010 809
4 2011 809
4 2012 809
;

data want;
    set have;
    by patient_id;
    if last.patient_id;
run;

 

--
Paige Miller
Tom
Super User Tom
Super User

COALESCE() or COALESCEC() if the variables are character should do it.

Just use a descending order variable list.

data want;
  set have;
   lastzip=coalescec(of zip2012-zip2008);
run;
quickbluefish
Barite | Level 11
This is interesting - I had no idea you could specify a variable list in descending order.
terjeph
Obsidian | Level 7
Simple and elegant!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 914 views
  • 2 likes
  • 4 in conversation