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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
Reeza
Super User

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;
K1235
Fluorite | Level 6

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

Reeza
Super User

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

ballardw
Super User

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

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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