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

I have this date variable (numeric, format DATE9.) that appears as 01DEC2011. I need to change that to this format: 12/29/2011.

 

I seem to be missing something in my code (see below) because i'm getting missing values for the new date variable. 

 

start_dt_n = INPUT(PUT(start_dt,10.),YYMMDD10.);
FORMAT start_dt_n MMDDYY10.;

 

Any hints on how to fix this?

 

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Try

start_dt_n = start_dt;
FORMAT start_dt_n MMDDYY10.;

or change the format of start_dt to mmddyy10.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

If the variable is really numeric and a true SAS date, then all you need to do is change the format of the variable. There are many ways this format change can be done, the simplest might be to re-create the data set using the desired format.

--
Paige Miller
andreas_lds
Jade | Level 19

Try

start_dt_n = start_dt;
FORMAT start_dt_n MMDDYY10.;

or change the format of start_dt to mmddyy10.

ballardw
Super User

@ama220 wrote:

I have this date variable (numeric, format DATE9.) that appears as 01DEC2011. I need to change that to this format: 12/29/2011.

 

I seem to be missing something in my code (see below) because i'm getting missing values for the new date variable. 

 

start_dt_n = INPUT(PUT(start_dt,10.),YYMMDD10.);
FORMAT start_dt_n MMDDYY10.;

 

Any hints on how to fix this?

 

Thanks 


if start_dt is already a SAS date value then a value equivalent to 01DEC2011 is numerically  18962. SAS dates are stored as the number of days from 01JAN1960. So put(start_dt,10.) would be "     18962". Note the leading blanks. That would be cause of your missing values and your LOG would have had a bunch of lines like:

NOTE: Invalid argument to function INPUT at line 104 column 3.
start_dt=18962 y=. _ERROR_=1 _N_=1

Formats are a very powerful and flexible tool in SAS. You change the analysis of groups of date values just by changing the format for the duration of a procedure. You do not have to make the change permanent.

 

An example of using different formats for different analysis with proc freq. Change HAVE to the name of your data set.

proc freq data=have;
   tables start_dt;
   format start_dt mmddyy10.;
run;

proc freq data=have;
   tables start_dt;
   format start_dt YYQ.;
run;

proc freq data=have;
   tables start_dt;
   format start_dt Year4.;
run;

One counts values by calendar date, next values by calendar quarter and the last by calendar year. Note that the permanent format is not affected (though will be applied if you create output sets using the out option). This can be done with almost any procedure.

 

And custom formats can be applied to your specific groups of values as well.

nevennett
Fluorite | Level 6

Hi, 

I'm using SAS on demand and I'm struggling to change a numeric to a SAS date. 

 

My numeric variable is SurgStart which as format best12. and informat best32. 

 

My code is:

data ab1;

set ab;
date_var=input(put(SurgStart, best12.),yyyymmdd.);
format date_var date9.;
run;

 

This doesn't work. What I want to happen is for SurgStart e.g.20140410 to become the date 2014-04-10 so I can then do time series analysis. 

 

Please help!

Why is this

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1114 views
  • 6 likes
  • 5 in conversation