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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1374 views
  • 2 likes
  • 5 in conversation