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;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.