BookmarkSubscribeRSS Feed
PatSB
Calcite | Level 5

Hello all,

 

I couldn't find a thread that could help me, I did look for about an hour though.

 

I have a character variable "DOB" in the form of MM-YYYY (i.e. 01-2000) that represents birthdays. I want to calculate the amount of time between a person's birthday and April 1, 2019 (it's the start of a specific fiscal year I'm interested in). The units (i.e. days, months, years) of the difference I calculate is not important (i.e. it could be anything) and rounding is OK too.

 

My line of thought was to 1) convert "DOB" into a date variable in the form MM/YYYY; 2) Generate a date variable for April 2019 in the form of MM/YYYY (i.e. dropping the 1 to facilitate differencing); and 3) difference the variable created in Step 1) from the variable created in Step 2).

 

However, I haven't been able to make any useful progress in doing this any thoughts or guidance would be appreciated. 

 

Regards,

PatSB

1 REPLY 1
PaigeMiller
Diamond | Level 26

If you don't have a day within the month, select the first of the month.

 

Use actual SAS dates (which are integers that represent the number of days since 01JAN1960). When you read in something like 202107 using informat of YYMMN6. this converts it to an actual SAS date. Do not try to do this with text strings, and do not leave 202107 as 202107. Use actual SAS dates.

 

Then, you can use the INTCK function to determine the number of months since April 2019. Do not try to write a function on your own that determines number of months since a certain date. SAS has done the hard work for you, so you don't have to figure out how many months are between two dates.

 

Example:

 

data fakedata;
     input fake_date yymmn6.;
     format fake_date yymms7.;
     cards;
202101 
202107
;
data want;
    /* Compute months since April 2019 */
   set fakedata;
   months = intck('month','01APR2019'd,fake_date);
run;

 

--
Paige Miller

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 288 views
  • 0 likes
  • 2 in conversation