SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
EpiNovice
Calcite | Level 5

Hi there,

 

I have a log form data set with a column for id, visit type and visit date. I want to create a new variable (0/1) indicating which visits have a date greater than the date for visit type='6' for each ID. So for example, in the simplified data below, I want to the drop the rows with bolded values because the dates for those visits come after the dates for visit type 6.

 

I thought I could do this with a simple combination of equation and IF/THEN statements, but I'm stumped. Suggestions?

IDVisit Type Date
1330-Mar
1630-Jun
1930-Sep
2015-Jan
2315-Mar
2615-Jun
3330-Apr
3630-Jul
386-Sep
4610-Aug
4710-Oct
41210-Dec



Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    merge have have(where=(visit_type=6) rename=(date=visit_type_6_date));
    by id;
    if date > visit_type_6_date then flag=1; else flag=0;
run;

I'm not sure why you have not bolded the third record, it seems like under the rules you have explained, that one should get dropped as well.

--
Paige Miller

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Are your dates true numeric SAS date values, formatted as shown? Or are they text as shown?


Will every ID value have one and only one visit type equal to 6? Could an ID have zero visit type records equal to 6? Could an ID have more than one visit type records equal to 6?

--
Paige Miller
EpiNovice
Calcite | Level 5

The dates are formatted as yymmdd10

PaigeMiller
Diamond | Level 26

Will every ID value have one and only one visit type equal to 6? Could an ID have zero visit type records equal to 6? Could an ID have more than one visit type records equal to 6?

--
Paige Miller
EpiNovice
Calcite | Level 5
Each ID only has a single visit type 6.
PaigeMiller
Diamond | Level 26
data want;
    merge have have(where=(visit_type=6) rename=(date=visit_type_6_date));
    by id;
    if date > visit_type_6_date then flag=1; else flag=0;
run;

I'm not sure why you have not bolded the third record, it seems like under the rules you have explained, that one should get dropped as well.

--
Paige Miller
EpiNovice
Calcite | Level 5
It should have been bolded, thank you for pointing that out.
Astounding
PROC Star

For this solution to work properly, you need to reverse the order in the merge:

 

data want;
    merge have(where=(visit_type=6) rename=(date=visit_type_6_date)) have;
    by id;
    if date > visit_type_6_date then flag=1; else flag=0;
run;

 

Otherwise, the values on the first observation for each ID will be wrong. 

EpiNovice
Calcite | Level 5
Thank you, I was wondering why there was something wrong with my new dataset.
Astounding
PROC Star
Why bother with dates? Would this work;

if VisitType > 6 then delete;
EpiNovice
Calcite | Level 5
Unfortunately no. The visit types are actually longer names, I just shortened them for simplicity.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1705 views
  • 0 likes
  • 3 in conversation