BookmarkSubscribeRSS Feed
lisa2002
Fluorite | Level 6

Team, 

 

I have read the codes that have been posted to the community page for retrieving the last observation however I am not receiving the results I need.  After inputting all lag statements I need to make sure to keep only the last observation for each patient and I have over 1000 so I would I go about doing this? 

 

Example 

 

name  date  prescription_1 prescription_2 Prescriber_name

 

and so on...after I completed my lag statements..

 

Code 

data test;
set sashelp.class end=eof;
if eof then output;
run;

 

or 

 

data test;
if 0 then set sashelp.class nobs=nobs end=eof;
set sashelp.class point=nobs;
output;
stop;
run;

 

Please Assist

5 REPLIES 5
ballardw
Super User

One example of getting the "last" of each age group using the sashelp.class data set.

 

proc sort data=sashelp.class
          out=work.class;
   by age ;
run;

data work.want;
   set work.class;
   by age;
   if last.age;
run;

One might guess that for a patient that the data should be sorted by patient identification and a SAS date value (not character or funny number that mimics a date, an actual SAS date value) such as

 

proc sort data=have
   by name date;
run;

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

Character valued dates seldom sort properly and the only date form not a SAS date that works well with this approach has a YYYYMMDD appearance.

 

Your mention of LAG makes me suspect you may have picked examples from a very complicated approach instead of a basic order of occurrence data example. If you need something other that sorting by personal identification and date then you will likely need to provide some example data and the rules involved.

lisa2002
Fluorite | Level 6

Thank you for your assistance, I am still stuck.  I will share more information, here is my original lag statement 

 

data work.temp2;
set work.sorted;
rx0=extract;
lagnum=lag1(number);
rx1=lag1(rx0);
if lagnum~=number then rx1="";

lagnum=lag2(number);
rx2=lag2(rx0);
if lagnum~=number then rx2="";

lagnum=lag3(number);
rx3=lag3(rx0);

if lagnum~=number then rx3="";

run;.....and it goes on until 25 

 

now I need the last row for each patient that contains their ....

 

rx_number and number and Patient names and rx_name which one patient can have upto 25 scripts

 

here is how I am trying to get that last row...

 

data pharmtest;
set work.temp2;
/***********By is USED FOR THE GROUPING PURPOSE*********/
by NUMBER ;
firstnumber = first.number;
lastnumber = last.number;
if lastnumber=1 then cnt=1;
else cnt+1;
run;

 

or would I have to do something like this??

 

data pharmtest;
set work.temp2;
/***********By is USED FOR THE GROUPING PURPOSE*********/
by NUMBER ;

*if last.rx_number and number~=number and PATIENTS=PATIENTS and rx_number~=rx_number and rx_name~=rx_name then do;
lagnum=lag1(number);
rx1=lag1(rx0);
if lagnum~=number then rx1="";

 

I don't think I would have to lag any more correct? 

Kurt_Bremser
Super User

SAS coding is (as most programming) data-driven. Without knowing what you have, and what you want to get out of it, we can only make (poorly educated) guesses.

Please provide example data in usable form (a data step with datalines), and what you expect to get from it.

Astounding
PROC Star
While the objective is unclear, I'm willing to guess that you are after this (after sorting) :

data want;
update have (obs=0) have;
by patient;
run;

This will select the last nonmissing value for each variable (per patient) which seems like it might be the objective.

You have the data, and the program is short, so see if this accomplishes what you need.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 563 views
  • 0 likes
  • 4 in conversation