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

Hello

I am using SAS 9.3.  I have a data set of all hospital visits for a single year by chart number (which is unique to the patient).  I want to report on the residence of each patient but I want the residence reported on to be from the most recent patient visit.  This is what I have so far:

data have;

input @1  chartno $6.
         @7  acctno  $3.
         @10 admdate yymmdd8.
         @18 sepdate yymmdd8.
        @26 postal $6.
   ;

format admdate yymmdd10. sepdate yymmdd10.;

cards;
1111110012012032920120331N0H2C1
2222220022012050120120515L4M6X6
3333330032012060120120607L4N4A8
1111110042012040220120410N4L1H4
4444440052012060320120615N4L1S7
5555550062012053120120603L0L2K0
2222220072012070120120713L4M6X6
2222220082012072020120725L9Y2M7
4444440092012081320120817N4L1S7
3333330102012061520120621L4N4A8
1111110112012110120121130N4L1H4
run;

proc sort in=have;
by chartno descending admdate;
run;

data want (drop=postal_lag);
set have;
by chartno;
format
    index_postal $6.
    ;
index_postal='';
postal_lag=lag(postal);
chart_lag=lag(chartno);

if first.chartno then index_postal=postal;
else index_postal=postal_lag;

 
run;

The result is:

Results.jpg

It seems to work for the occurrence after the first (i.e. chart 111111 and 222222) but not on the 3rd occurrence of 222222.  Any assistance is greatly appreciated.  Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Fugue
Quartz | Level 8

If you only want the final data set to contain the postal code for a chartno based on the last (most recent) admission date, then replace your last data step with this:

data want;
set have;
by chartno;

retain last_postal;

if first.chartno then last_postal = postal;

run;

View solution in original post

2 REPLIES 2
Fugue
Quartz | Level 8

If you only want the final data set to contain the postal code for a chartno based on the last (most recent) admission date, then replace your last data step with this:

data want;
set have;
by chartno;

retain last_postal;

if first.chartno then last_postal = postal;

run;

shellp55
Quartz | Level 8

Thank you so much!!  That is exactly what I wanted! 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 893 views
  • 0 likes
  • 2 in conversation