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

How would I create a variable that says 'Y' to every patient who has attempted to do something every 6 months? 

 

For example:

 

Patient_ID      Date.                  Activity

1234               20FEB2020      Football

1234               20FEB2020      Basketball

1234               25FEB2020      Ski

1234               07SEP2020      Baseball

 

I want the new variable to say "Y" for the 20FEB2020, 'N' for the second '20FEB2020' 25FEB2020, and 'Y' for 07SEP2020.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@mar0000 wrote:

How would I create a variable that says 'Y' to every patient who has attempted to do something every 6 months? 

 

For example:

 

Patient_ID      Date.                  Activity

1234               20FEB2020      Football

1234               20FEB2020      Basketball

1234               25FEB2020      Ski

1234               07SEP2020      Baseball

 

I want the new variable to say "Y" for the 20FEB2020, 'N' for the second '20FEB2020' 25FEB2020, and 'Y' for 07SEP2020.


So it does not matter what the value of activity may be just that it is present? Is activity ever missing in this data?

 

Something like this could get you started, but missing value for the activity can mess with this.

data have;
input Patient_ID $   date  Date9.     Activity $;
format date date9.;
datalines;
1234 20FEB2020 Football
1234 20FEB2020 Basketball
1234 25FEB2020 Ski
1234 07SEP2020 Baseball
;
data want;
   set have;
   by Patient_ID date;
   dd = intck('month',lag(date),date,'C');
   if first.Patient_ID then flag=1;
   else if dd < 6 then flag=0;
   else flag=1;
   drop dd;
run;

Note that the code is posted in a code box opened with the </> icon to preserve formatting. Text pasted into the main message windows can have all sorts of odd things get removed or inserted. The data step shown is the way to provide data. Then there is no question about the types of variables involved. Dates, times and datetime values are especially sensitive. We sometimes spend lots of questions about "not working" code only to find out the "date" is not an actual SAS date value so the format or function attempted never had a chance of working correctly.

 

BTW I would suggest coding the value as numeric 1 instead of 'Yes' and 0 instead of 'No'. It can be much easier to "count" things coded this way. Summing a variable gives you a number of Yes, the mean gives the percent of yes, Range tells if the value ever changed, max tells that at least one value was set and few other neat tricks depending on the type of questions asked.

View solution in original post

3 REPLIES 3
mar0000
Obsidian | Level 7
Also, a 'N' for the 25FEB2020. Sorry, forgot to put that
PaigeMiller
Diamond | Level 26

Every six months, starting when?

--
Paige Miller
ballardw
Super User

@mar0000 wrote:

How would I create a variable that says 'Y' to every patient who has attempted to do something every 6 months? 

 

For example:

 

Patient_ID      Date.                  Activity

1234               20FEB2020      Football

1234               20FEB2020      Basketball

1234               25FEB2020      Ski

1234               07SEP2020      Baseball

 

I want the new variable to say "Y" for the 20FEB2020, 'N' for the second '20FEB2020' 25FEB2020, and 'Y' for 07SEP2020.


So it does not matter what the value of activity may be just that it is present? Is activity ever missing in this data?

 

Something like this could get you started, but missing value for the activity can mess with this.

data have;
input Patient_ID $   date  Date9.     Activity $;
format date date9.;
datalines;
1234 20FEB2020 Football
1234 20FEB2020 Basketball
1234 25FEB2020 Ski
1234 07SEP2020 Baseball
;
data want;
   set have;
   by Patient_ID date;
   dd = intck('month',lag(date),date,'C');
   if first.Patient_ID then flag=1;
   else if dd < 6 then flag=0;
   else flag=1;
   drop dd;
run;

Note that the code is posted in a code box opened with the </> icon to preserve formatting. Text pasted into the main message windows can have all sorts of odd things get removed or inserted. The data step shown is the way to provide data. Then there is no question about the types of variables involved. Dates, times and datetime values are especially sensitive. We sometimes spend lots of questions about "not working" code only to find out the "date" is not an actual SAS date value so the format or function attempted never had a chance of working correctly.

 

BTW I would suggest coding the value as numeric 1 instead of 'Yes' and 0 instead of 'No'. It can be much easier to "count" things coded this way. Summing a variable gives you a number of Yes, the mean gives the percent of yes, Range tells if the value ever changed, max tells that at least one value was set and few other neat tricks depending on the type of questions asked.

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