I have a variable that I've named time_period which contains dates listed in the following format as a numeric variable:
201511
201511
201612
201701
201701
...
SAS reads these as a "SAS date" and therefore converts 201511 (which is intended to be Nov 2015) to Sep 20, 2511.
I found this out when I tried formatting to a word date so I can understand how SAS was reading it:
data mydata;
set sample;
sasdate=input(time_period, YYMON.); /*YYMMDDw.*/
format sasdate worddate12.;
run;
From what I understand, I need to take this numeric value and convert it to a date value. Some responses explain that you need to ad an "01" in the code to act as a "dd", so I tried the following:
data cdiff.date;
set cdiff.c;
format sasdate date9.;
sasdate = input(time_period !! "01", yymmdd8.);
run;
This did not work, the output is "." down the entire column.
Other examples suggest adding a specific date in the date field (I think), but that replaces all the dates with this specific date in my new column.
I am not sure how to approach this.
Thanks
I don't think operators (like !!) are allow in arguments to the INPUT function, but the CATS function is. Use
sasdate=input(cats(time_period,'01'),yymmdd8.);
I don't think operators (like !!) are allow in arguments to the INPUT function, but the CATS function is. Use
sasdate=input(cats(time_period,'01'),yymmdd8.);
You are absolutely brilliant, this is exactly what I needed. Thank you!
If you have numbers like 201,511 and you want to convert that to the date 01NOV2015 there are a number of ways.
You could use arithmetic to extract the YEAR and MONTH values.
date = mdy(int(number/100),1,mod(number,100));
You could convert the number to a string and use the YYMMN informat to convert it.
date=input(put(number,z6.),yymmn6.);
Once you have a date value attach any date type format you want to display it in a way that humans can understand, preferably one that will not be confused for a number like the 201,511 you started with.
Possibly go back to your step where you read the data into SAS and read that variable with the YYMMN. informat at the beginning so the value starts as a date. The YYMMN. informat will treat the resulting date as the first day of the month.
data example; input date :yymmn.; format date yymmdd10.; datalines; 201511 201511 201612 201701 201701 ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.