BookmarkSubscribeRSS Feed
jleader13
Calcite | Level 5

Hi all,

 

I am looking at multiple urine samples from multiple pregnancies and only want to keep those from the last pregnancy (this could be multiple samples from the last pregnancy). Currently using this code but it's not working, it seems to be only keeping the last observation from each pregnancy. Any help would be greatly appreciated! 

 

data preg1;

set preg;

by ID2 pregcycle;

if LAST.pregcycle then output;

run; 

2 REPLIES 2
ballardw
Super User

It might help if you provide a description of what in your data would actually identify the "last" pregnancy.

With a time word like "last" I might expect a date variable would be involved if you mean "latest". If you mean "last" in the "from previous" that is likely a bit more work.

 

Best would be to provide some example data, or something that at least looks and behaves your data with personal identifiable items removed if not needed or masked some way (values like Minnie Mouse for names for example). Best is to provide the example in the form of data step code that we can use to test possible solutions. The data step helps because then we do not need to guess if a variable is numeric or character. Along with the example data provide, based on that example, the expected output. The examples do not need to be very big, just large enough to demonstrate all of the possible issues.

 

If Pregcycle is something like a number related to sequence of pregnancy and each Pregcycle has multiple records such as different doctor visits then the result you describe is exactly what your code asked for.

Consider some dummy data:

ID  Pregcycle  visitnumber
1   1          1
1   1          2
1   1          3
1   2          1
1   2          2
1   2          3

The Last.Pregcycle would identify the visitnumber = 3 in both cycles because that record is "last" of the Pregcycle values.

Reeza
Super User
data preg1;

set preg;

by ID2 pregcycle;

if LAST.ID2;

run; 

last.pregcycle picks the last record from each pregcycle. If you want last pregnancy, get the last per ID. If you have multiple records for the last pregnancy this won't work, it will keep only the last line. 

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