DATA Step, Macro, Functions and more

Change variable from numeric to SAS date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Change variable from numeric to SAS date

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 


Accepted Solutions
Solution
2 weeks ago
Valued Guide
Posts: 525

Re: Change variable from numeric to SAS date

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


All Replies
Respected Advisor
Posts: 2,836

Re: Change variable from numeric to SAS date

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
Solution
2 weeks ago
Valued Guide
Posts: 525

Re: Change variable from numeric to SAS date

Try

start_dt_n = start_dt;
FORMAT start_dt_n MMDDYY10.;

or change the format of start_dt to mmddyy10.

Super User
Posts: 13,358

Re: Change variable from numeric to SAS date


@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.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 116 views
  • 6 likes
  • 4 in conversation