BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasg
Calcite | Level 5

Hi All,

 

I’m trying to assign visit names based on the 

dates. Need help regarding logic. Using SAS 9.3.

i have dataset a, one record per member.

id     Date

001  28jan2016

002   04feb2017

 

dataset b: multiple records per member.need to derive

visit value based on dataset a date value. Date-st is

before the date then visit values should be visit-1,visit-2

Etc...if Date-st is after the Date then visit values should be 

visit+1 , visit+2 etc. Different number of visits per subject

. If Date-st is missing then need to use Date-ed to 

assign visit.

id      Date-st        Date-ed         Visit

001   27jan2016    29jan2017    Visit-1

001    28jan2016   29jan2017    Visit0

001   29jan2016    30jan2017    Visit+1

002    4feb2017     6feb2017    Visit0

002.   6feb2017    7feb2017      Visit1

002    8feb2017    9feb2017      Visit2

003            .           10feb2017      Visit3

 

As I just started on SAS, Need help me with the coding.thanks for your help.    

 

Thanks in advane.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Is there any real reason to include "visit" in the variable if the text is in every single value?

Also please confirm that your dates as SAS date valued numeric variables with a SAS date9. format applied.

 

Note that you have made things somewhat difficult by not giving us the names of the datasets and using variable names that by default are not valid for SAS variables (date-st date-ed, hyphens aren't normally valid SAS variables).

Also for id 002 you compare 04feb2017 with

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Is there any real reason to include "visit" in the variable if the text is in every single value?

Also please confirm that your dates as SAS date valued numeric variables with a SAS date9. format applied.

 

Note that you have made things somewhat difficult by not giving us the names of the datasets and using variable names that by default are not valid for SAS variables (date-st date-ed, hyphens aren't normally valid SAS variables).

Also for id 002 you compare 04feb2017 with

 

sasg
Calcite | Level 5
 
PGStats
Opal | Level 21

It can be done like this:

 

proc sort data=b; by id date_st date_ed; run;

data want;
length visit $10;
nn = 0;
do until (last.id);
    merge a b; by id;
    nn = nn - (coalesce(date_st, date_ed) < date);
    end;
do until (last.id);
    merge a b; by id;
    if coalesce(date_st, date_ed) = date then nn = 0;
    visit = cats("Visit", nn);
    output;
    nn = nn + 1;
    if nn = 0 then nn = 1; /* Skip visit0 if date is not found */
    end;
drop nn;
run;
PG
sasg
Calcite | Level 5
Thanks for your reply.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1315 views
  • 0 likes
  • 3 in conversation