Hi. I need help. I read everything I could, even prior SAS community sites and Cody's books. I'm stuck. please help me.
I have 60 patients. Together they have 1624 visits. Each has a different number of visits.
Here are my variables:
Alphabetic List of Variables and Attributes
# Variable Type Len Format
age_q_visit (N-Number)
id (N)
q_oxyconcent (N)
q_oxytime (N)
q_post_gluc (N)
q_pre_gluc (N)
q_visitdate Date MMDDYY8.
visitno (N) they are in order by date
This is what I want, but won't work:
MY GOAL - TO ISOLATE THE LAST VISIT NUMBER AND AGE WHILE KEEPING q_visitdate and ID
proc sort data=have; by id visitno; run;
data hope;
set have;
if last.visitno;
retain id q_visitnum;
run;
ANY IDEAS?
Thanks.
set hope;
Would need some test data in the form of a datastep and what you want to see at the end. For instance, what does "last age" mean? Do you just want maximum age from the data? What does last visit mean? Its really hard to guess, but if last visit just means sorting it and taking the last record:
proc sort data=have out=want1 (obs=1); by descending visitno; run; proc sort data=have out=want2 (obs=1); by descending age; run;
That will give the last visit and last age.
Dear Super User,
Thank you! However, it did not work. I want to save id and last clinicvisitdate (a date var).
So I need both the ID and the last clinicvisitdate for 60 patients.
SO I want an output like this:
id lastclinicvisitdate
1 12/14/15
2 2/9/18
etc.
Please refer to post guidance, provide test data in the form of datastep and what you want out:
I don't understand that reading at all!
@mgrzyb wrote:
I don't understand that reading at all!
Then what are you doing here?
Frankly, that 6-step instruction is so easy to follow that not being able to do that bodes very badly for your future in SAS programming.
@Kurt_Bremser wrote:
Frankly, that 6-step instruction is so easy to follow that not being able to do that bodes very badly for your future in SAS programming.
Maybe.
But I think the process could still be easier. I will write a post about this on the "Community Matters" board, probably later today.
Hi @mgrzyb,
I think your code requires only a few modifications. The PROC SORT step was good. The sorted HAVE dataset will have the largest VISITNO per ID in the last observation of the respective ID. So, in a subsequent data step we can go through the dataset using the SET statement (as you've suggested) and select the last observation from each block of consecutive observations with the same ID ("BY group" in SAS jargon).
The subsetting IF condition you used ("if last.visitno;") might sound right in a sense (after all, we do want to select the last VISITNO), but it's inappropriate. The "last." in this syntax refers to the BY group and hence must be followed by the name of the BY variable defining that group, in our case: ID. Moreover it requires a BY statement to specify the BY variable(s).
Finally, the KEEP statement is the right tool to keep selected variables in an output dataset, not the RETAIN statement.
Now, try this modified data step (after your PROC SORT step):
data want;
set have;
by id;
if last.id;
keep id q_visitdate age_q_visit;
run;
As a kind of safety measure you could also replicate the BY statement from your PROC SORT step in the above data step, i.e. include the second BY variable, VISITNO. This wouldn't change the result, but if you forgot to run the PROC SORT step, it would cause an error message in the log even if HAVE was sorted by ID, but not by ID VISITNO. I've included age_q_visit in the KEEP statement because you mentioned "age" in your initial post.
Hi Freelance Rienhard,
That code did it! Thank you!
For the others, please explain what 6 lines of code!!!! Please. I want to understand!
M.
@mgrzyb wrote:
Hi Freelance Rienhard,
That code did it! Thank you!
For the others, please explain what 6 lines of code!!!! Please. I want to understand!
M.
@mgrzyb The 6 step instructions on how to post your data as a data step that was previously linked to. Could you please elaborate on what you cannot follow on this post and what steps gave you issues? Your statement was:
I don't understand that reading at all!
You seem to be interpreting things incorrectly (language barrier?), regarding the post (writing vs reading) and "6 lines of code" versus "6 steps" which are very different things.
The link is here:
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.