BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

Hi everyone,

Here are the variables I am working with:

ID
Assessment Date
Event Date

I need a variable to tell me which session that date corresponds to (Baseline, Follow Up, and Last Session).  The issue is that there are cases that only have 1 session and these 1 sessions are getting lumped in with the follow up sessions. Here is what I’ve done so far.

  1. Use the first and last function in SAS to create a FirstDate and LastDate variable using ChildID and Assessment date.
  2. If FirstDate = AssessmentDate Then session = Baseline
  3. If LastDate = EventDate Then session = Last Session

data Want; set Have;

by ID EventDate ;

if first.ID then FirstDate = AssessmentDate;

if last.ID then LastDate = AssessmentDate;

 

if FirstDate = AssessmentDate then Session = 1; /*Baseline*/

If LastDate = AssessmentDate then Session = 3; /*Last Session*/

 

 

 

Both of the codes below lumps those with only 1 session in with the sessions that should be follow up:

 

if FirstDate = LastDate and AssessmentDate = EventDate = then session = 4; /*Only 1 session*/

if firsthospdate NE assessmentdate_HW and firsthospdate=. and lasthospdate=. then Session = 2; /*Follow up*/  

 

Any help would be greatly appreciated!

1 REPLY 1
ballardw
Super User

Look up ELSE for use with IF-Then.

The ELSE part only gets assigned if the first is not true. Your code is evaluating both cases every time.

And since you are using a variable LASTDATE that should only have a value when LAST.ID you only want them evaluated when LAST.ID. So consider

If Last.Id then do;

   LastDate = AssessmentDate;

 

 

   If LastDate = AssessmentDate then Session = 3; /*Last Session*/

   if FirstDate = LastDate and AssessmentDate = EventDate = then session = 4; /*Only 1 session*/

   else if firsthospdate NE assessmentdate_HW and firsthospdate=. and lasthospdate=. then Session = 2;

End;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1 reply
  • 667 views
  • 0 likes
  • 2 in conversation