DATA Step, Macro, Functions and more

SAS Char date YYYY:M into Date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

SAS Char date YYYY:M into Date

Hello, I have a variable Date in Char format. I would like to convert that into Date format. 

 

Date

----------

2017-8

2016-7

 

Please help!

Thanks!


Accepted Solutions
Solution
‎09-14-2017 12:24 PM
Super User
Posts: 19,861

Re: SAS Char date YYYY:M into Date

What do you want the day to represent, 1, 15, last day of the month?

 

Use the MDY() function is probably the easiest here. I broke out how to create the parts if necessary.

 

 data have;
 char_date = "2017-8";output;
 char_date = "2017-10";output;
 run;

 data want;
 set have;

 year_char = scan(char_date, 1, "-");
 year_num = input(year_char, 8.);
 *same for month, or nest it all in one;
 date_sas = mdy(input(scan(char_date, 2, "-"), 8.) , 1, input(scan(char_date, 1, "-"), 8.));

 format date_sas yymon7.;
 run;

View solution in original post


All Replies
Solution
‎09-14-2017 12:24 PM
Super User
Posts: 19,861

Re: SAS Char date YYYY:M into Date

What do you want the day to represent, 1, 15, last day of the month?

 

Use the MDY() function is probably the easiest here. I broke out how to create the parts if necessary.

 

 data have;
 char_date = "2017-8";output;
 char_date = "2017-10";output;
 run;

 data want;
 set have;

 year_char = scan(char_date, 1, "-");
 year_num = input(year_char, 8.);
 *same for month, or nest it all in one;
 date_sas = mdy(input(scan(char_date, 2, "-"), 8.) , 1, input(scan(char_date, 1, "-"), 8.));

 format date_sas yymon7.;
 run;
Occasional Contributor
Posts: 7

Re: SAS Char date YYYY:M into Date

Sorry, I want to just output yyyy-mm no day. Also, your second char_date outputed 2017JAN not 2017OCT

Super User
Posts: 19,861

Re: SAS Char date YYYY:M into Date

ah..that's because there's a problem with the test data - note the answer though. I didn't assign a variable length so SAS defaults to the first it found, so it read it as 2017-1, not 2017-10.

A SAS date has to have a day component, though you can show only the month year as shown.

 

Anyways, find the format you want and change the format statement to what you want to see. 

The formats are here:

 

https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm

Super User
Posts: 11,343

Re: SAS Char date YYYY:M into Date

SAS will require a day of month as part of the value to be treated as a "date" variable for any of the functions.

Best might be to read with the anydtdte. format.

data  example;
   input x $;
   y = input(x,anydtdte.) ;
   format y yymmd7.;
   yy=year(y);
   mm=month(y);
   dd=day(y);
datalines;
2017-8
2016-7
2016-10
;
run;

Note that the format I assigned will mimic the input values only the month will always have two digits.

 

Note that other SAS defaults can apply when reading quarters into dates where the month and day are the first of the calendar quarter

Super User
Posts: 5,516

Re: SAS Char date YYYY:M into Date

There is no such thing.  In SAS, dates always refer to a specific day.  Always.  You can change the value of your character variable if you would like:

 

data want;

length date $ 7;

set have;

if length(date)=6 then date = cats(substr(date,1,5), '0', substr(date, 6,1));

run;

 

That creates DATE as a character string, with slightly different values than what it had before.  But if you want what SAS considers to be a date, this is not it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 126 views
  • 3 likes
  • 4 in conversation