Hi! How do i change a variable (X) with a date format mmddyy10. to a SAS date?
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.
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.
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
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;
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.
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.
@Astounding Ok, but how do you remove the date format to just dispaly the number (w/o doing the subtraction)?
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;
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.
For display or output, you can do this in a data step:
put x best.;
That saves doing the conversion and creating another variable.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.