BookmarkSubscribeRSS Feed
jmarnaez
Fluorite | Level 6

Sorry if the title isn't quite accurate, but it's the most succinct way I could think. I have a large dataset in which some individuals have multiple tests (each individual is given a unique ID). Some of my team members are interested in identifying time to follow up on elevated tests and I'm not quite sure how to do that in SAS. Functionally I need to create a variable that indicates whether the previous test for that ID was elevated in cases where an ID has multiple tests on different dates. Any help with this would be greatly appreciated.

4 REPLIES 4
mkeintz
PROC Star

Please show some sample data, in the form of a working data step.

 

And the corresponding desired output.

 

Help us help you.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jmarnaez
Fluorite | Level 6

I do not have access to SAS right now to test the data step, I'll provide what some truncated data looks like and what I'd like the end product to be. 

data test;

input ID TestDate Elevated $;

datalines;

1 20200101 Y

1 20200105 N

1 20200110 N

2 20200106 Y

2 20200108 N

3 20200112 Y

3 20200114 Y

3 20200125 Y;

run;

ID                   TestDate                     Elevated

1                     20200101                   Y

1                     20200105                   N

1                     20200110                   N

2                     20200106                   Y

2                     20200108                   N

3                     20200112                   Y

3                     20200114                   Y

3                     20200125                   Y

 

What I'd like the data to look like with the new variable is this

ID                   TestDate                     Elevated        Prior Elevated

1                     20200101                   Y                    NA

1                     20200105                   N                    Y

1                     20200110                   N                    N

2                     20200106                   Y                    NA

2                     20200108                   N                    Y

3                     20200112                   Y                    NA

3                     20200114                   Y                    Y

3                     20200125                   Y                    Y

andreas_lds
Jade | Level 19

Something like

data want;
   set test;
   by Id;
   
   prior_elevated = lag(Elevated);
   
   if first.id then prior_elevated = ' ';
run;

should do it.

mkeintz
PROC Star

Thanks for posting sample data.

 

The response by @andreas_lds will certainly produce your sample output.

 

But how does your sample output provide "time to follow up on elevated tests" which you described as your teammates' interest in your original post?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 374 views
  • 1 like
  • 3 in conversation