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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

9 REPLIES 9
ballardw
Super User

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.

jenim514
Pyrite | Level 9

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

jenim514
Pyrite | Level 9

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;

ballardw
Super User

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.

Astounding
PROC Star

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.

jenim514
Pyrite | Level 9

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

Tom
Super User Tom
Super User

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;

 

Astounding
PROC Star

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.

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
  • 9 replies
  • 1904 views
  • 2 likes
  • 5 in conversation