BookmarkSubscribeRSS Feed
MerAgSo
Calcite | Level 5

Hello everyone, 

I am having a little bit of trouble with some dates. I uploaded an excel and imported it to a SAS database with dates in format dd/mm/yyyy. I used the following code to change it to SAS date but I get missings in return. I have tried DATE9. and DDMMYY10.

 

Here is the code: 

data dates;
set previous;
admdate=input(adm, DDMMYY8.);
format admdate;

run; 

Thank you

 

 

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Show us the unformatted values of ADM in your SAS data set named PREVIOUS.

--
Paige Miller
pink_poodle
Barite | Level 11
They are already a SAS date. You can just format admdate date9., and that should change the display.
Tom
Super User Tom
Super User

Before changing anything check what type of variable you have in the SAS dataset first. 

  • Is it numeric or character? 
    • If numeric does it have a format attached to it? 
      • Is it a date type format?  Like DATE, DDMMYY, MMDDYY, YYMMDD, etc?  
        • If so then there is nothing to do as you already have date values.  You could use a FORMAT statement to attach a different format so they dates are displayed differently.
      • No.  What do the values look like?  Look at some of them?  Are they number like 13,052,022 that a human might think of as a date in DDMMYYYY style?   
        • If so then first convert them to a string and then back to a date.  input(put(ADM,Z8.),ddmmyy8.)
    • If character then what do the values look like. 
      • Are they strings like '13052022' that a human might recognize as a date in DDMMYYYY style?
        • If so then your code should have worked.  Not your code will NOT work if the variable is numeric.
      • Are the strings strange digit strings that look like '44760'?
        • If so then they are the raw numbers that EXCEL uses as dates converted into digit strings.  That happens when the column has some cells with dates and others with strings.
          • Convert them to a number and correct for difference in how SAS and Excel count days.  input(adm,32.)+'30DEC1899'd

 

MerAgSo
Calcite | Level 5

It is in numeric format and in DDMMYYYY10.

The thing is that I want to change it to SAS date format. 

Thank you

Tom
Super User Tom
Super User

A FORMAT in SAS is instructions for how to display values as text.  If the variable is numeric then you have numeric values in the variable. 

 

Since the variable is numeric already you can replace the existing number with the date value and then just attach the format you want to use to display it.  

 

So if you have a dataset named HAVE with a numeric variable named DATE with values like 13,102,021 and 01,051,989 then you can use a step like this to create a new dataset named WANT that has the numbers converted into dates that will display by default as string in MM/DD/YYYY pattern.

data want;
  set have;
  date=input(put(date,z8.),mmddyy8.);
  format date mmddyy10.;
run;

 

Tom
Super User Tom
Super User

@MerAgSo wrote:

It is in numeric format and in DDMMYYYY10.

The thing is that I want to change it to SAS date format. 

Thank you


There is no SAS format named DDMMYYYY.   There is one named DDMMYY and others with a 7th letter to indicate the type of separator to use between the day month and year values.

 

And what does the 10 mean there?  If you have a series of digits in DDMMYYYY style the longest it can be is 8 characters.  The only reason you would need 10 is to have room for the delimiter.  But there is no place to store a delimiter in a numeric variable.

 

Did you just mean that the digits of the number look like they could be the day,month and year of a date value?  Then convert the number to a string and then back to a date value using PUT() and INPUT() functions.

date=input(put(date,z8.),ddmmyy8.);

 

And if you mean that the variable is numeric and has the DDMMYY format attached to it already then you already have date values.  So what is it you want to change?  Do you want to display the values in a different way?  Then use a FORMAT statement to attach a different format to the variable.

MerAgSo
Calcite | Level 5

Sorry, the format is DDMMYY10. considering separators (/). I know this a date format but I want the numeric value given for SAS date value (The numeric value which counts days from January 1st, 1960). I need this in order to estimate several day differences among dates. 

 

Tom
Super User Tom
Super User

@MerAgSo wrote:

Sorry, the format is DDMMYY10. considering separators (/). I know this a date format but I want the numeric value given for SAS date value (The numeric value which counts days from January 1st, 1960). I need this in order to estimate several day differences among dates. 

 


It is the number of days already.  To get the difference in days between two dates just subtract.

days_since = date2 - date1 ;
days_since_1960 = date ;
days_since_2000 = date - '01JAN2000'd ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 1063 views
  • 0 likes
  • 4 in conversation