The SAS Output Delivery System and reporting techniques

change date format to a sasdate

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 98
Accepted Solution

change date format to a sasdate

Hi!  How do i change a variable (X) with a date format mmddyy10. to a SAS date?

 

 


Accepted Solutions
Solution
‎09-27-2016 12:52 PM
Respected Advisor
Posts: 4,624

Re: change date format to a sasdate

There are a few variations involved in your question.  

 

If you are just using the date in calculations in a DATA step, you don't need to remove the format.  Even if these variables are formatted, the calculation works:

 

duration = date_end - date_start;

 

If you are printing the date and want to see the unformatted date, add a FORMAT statement to the PROC PRINT:

 

format x;

 

That's the general way to remove a format from a variable.  If you remove the format within a SAS procedure, that's a temporary change for that procedure only.  If you remove the format in a DATA step, the change is permanent.  And as Tom mentioned, you can use PROC DATASETS instead of a DATA step to add or remove a format assignment.  That would run quickly and would save the time it takes the DATA step to run.

View solution in original post


All Replies
Grand Advisor
Posts: 9,682

Re: change date format to a sasdate

Are you sure that your current variable is not a SAS date? Run proc contents or examime the column properties. If the format is mmddyy10. then it is already a SAS date variable.

Frequent Contributor
Posts: 98

Re: change date format to a sasdate

Sorry, let me clarify.  The variable is a SAS formatted date, but I want to change it to the numeric digits that calculates from 1960

Frequent Contributor
Posts: 98

Re: change date format to a sasdate

Nvmd.  I just answered my own question.

 

Just did a simple substraction to make my like easier.

 

data test;
set exp.fy09;
date=encdate1-'01jan1960'd;
run;

Grand Advisor
Posts: 9,682

Re: change date format to a sasdate

Even easier: Change the format to best8. or similar.

You don't need to change anything in the actual data you could just apply the format in what ever procedure, such as proc print.

Respected Advisor
Posts: 4,624

Re: change date format to a sasdate

Here's another way to look at it.  You just subtracted zero ('01Jan1960'd) from your variable, and came up with the right number.  Surely your original variable must have contained the right number to begin with ... it's just that you couldn't see it because of the format.

Frequent Contributor
Posts: 98

Re: change date format to a sasdate

[ Edited ]

@Astounding Ok, but how do you remove the date format to just dispaly the number (w/o doing the subtraction)?

Super User
Super User
Posts: 5,960

Re: change date format to a sasdate

That is waht the FORMAT statement does. You can change the format permanently attached in the data set by using the FORMAT statement in a data step (or by using PROC DATASETS to modify an existing data step).

 

Or you can just add the FORMAT statement to the proc you are using to look at the data.

 

proc means ;

  var mydate;

  format mydate ;

run;

 

Solution
‎09-27-2016 12:52 PM
Respected Advisor
Posts: 4,624

Re: change date format to a sasdate

There are a few variations involved in your question.  

 

If you are just using the date in calculations in a DATA step, you don't need to remove the format.  Even if these variables are formatted, the calculation works:

 

duration = date_end - date_start;

 

If you are printing the date and want to see the unformatted date, add a FORMAT statement to the PROC PRINT:

 

format x;

 

That's the general way to remove a format from a variable.  If you remove the format within a SAS procedure, that's a temporary change for that procedure only.  If you remove the format in a DATA step, the change is permanent.  And as Tom mentioned, you can use PROC DATASETS instead of a DATA step to add or remove a format assignment.  That would run quickly and would save the time it takes the DATA step to run.

Esteemed Advisor
Posts: 5,920

Re: change date format to a sasdate

For display or output, you can do this in a data step:

put x best.;

That saves doing the conversion and creating another variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Post a Question
Discussion Stats
  • 9 replies
  • 476 views
  • 2 likes
  • 5 in conversation