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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.);

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

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.);

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mmaximos
Obsidian | Level 7

You are absolutely brilliant, this is exactly what I needed. Thank you!

Tom
Super User Tom
Super User

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.

ballardw
Super User

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
;

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
  • 4 replies
  • 492 views
  • 3 likes
  • 4 in conversation