BookmarkSubscribeRSS Feed
wernie
Quartz | Level 8

I have a dataset where I use an informat of anydtdte21. and format MMDDYYs10. when I read in the file. The date field is displayed as something like 10/30/2008. I want to convert that to a character field that would look like 10302008 for a char_date field.

 

After doing that, I want to pull out the month, day, and year, so I have separate fields (char_mo, char_day, char_yr) that would be like 10, 30, and 2008 for this example.

 

I've tried a few things with put, input, and the datepart function, but I can't seem to get anything to work. Any help is appreciated. Thanks!

5 REPLIES 5
PaigeMiller
Diamond | Level 26

The whole idea of converting dates to characters really freaks me out, as I can not see any reasonable purpose to have this information as character strings. And so you might really want to think about why is this necessary, and perhaps come to the conclusion that it is not necessary.

 

The put command will convert numerics to characters.

 

chardate=put(numericdate,mmddyy8.);

From there it should be easy enough to get the month and year, if you really need that.

--
Paige Miller
Reeza
Super User
You rarely need to do this in SAS, especially as characters. You're of course aware that months and days will sort as 1, 11, 12, 13...19, 2, 20, 21 etc. because that's the character order?
Tom
Super User Tom
Super User

First point I want to make is that it is a mistake to store you character version of your dates using style of MMDDYYYY. They won't sort properly.  They are easily confused with values in DDMMYYYY order instead.  If you are storing dates as characters use YYYYMMDD order so that they sort and you don't confuse half your audience about what date you mean.

 

You can use the PUT() function to convert a value to a string.  Just use the format you want to display it with.  

date_char=put(date_val,yymmddn8.);

To get the day, month, year part of a date use the DAY(), MONTH() and YEAR() function.  But there are also DAY. , MONTH. and YEAR. formats.  The problem with the formats is they do not add the leading zero.

data test;
  date_val=today();
  format date_val date9.;
  date_char=put(date_val,yymmddn8.);
  day_val=day(date_val);
  month_val=month(date_val);
  year_val=year(date_val);
  day_char=put(day_val,z2.);
  month_char=put(month_val,z2.);
  year_char=put(year_val,z4.);
  output;
  day_char=put(date_val,day2.);
  month_char=put(date_val,month2.);
  year_char=put(date_val,year4.);
  output;
run;
proc print; run;
                     date_                 month_                            month_    year_
Obs     date_val      char      day_val      val     year_val    day_char     char     char

 1     17JUL2019    20190717       17         7        2019         17         07      2019
 2     17JUL2019    20190717       17         7        2019         17          7      2019
Kurt_Bremser
Super User

Out of experience:

DON'T!

There's functions for extracting the date parts (year(), month() and day()), and if you ever need a character representation, use one in ymd order, with or without hyphens (done with the e8601da and b8601da formats).

wernie
Quartz | Level 8

Thanks everyone! I was replicating a process that someone else used and they did that, so I was trying to do the same thing. I'm not entirely sure why they did that though as it looks like they ended up putting it back into a regular date and just used the format that I used way back when I imported the data. I'll keep looking through to see if it's really necessary, as mentioned.

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
  • 5 replies
  • 542 views
  • 6 likes
  • 5 in conversation