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

Hello

I want to convert SAS date to number (numeric).

For example:

SASDate=’28OCT2019’d;

NumberDate will be 20191028 (number)

What is the way to do it please?

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Again, change the format..

 

data _null_;
   SASDate='28OCT2019'd;
   n=input(put(SASDate, yymmn4.), 8.);
   put n=;
run;

Also, I very much agree with @PaigeMiller 

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

There you go

 

data _null_;
   SASDate='28OCT2019'd;
   n=input(put(SASDate, yymmddn8.), 8.);
   put n=;
run;
Ronein
Meteorite | Level 14

Thanks .Can you please let me know if the following are correct?

What will be the code if the desired numeric value is 28102019?

Number_date= input(put(SAS_Date,DDMMYYn8.),8.);

 

What will be the code if the desired numeric value is 1910?

Number_date= input(put(SAS_Date,YYMMN4.),4.);

 

Can you please explain what each statement is doing?

step1:put(SAS_Date,yymmddn8.)

Step2:input(Result of 1st step,8.)

PeterClemmensen
Tourmaline | Level 20

Simply change the format

 

data _null_;
   SASDate='28OCT2019'd;
   n=input(put(SASDate, ddmmyyn8.), 8.);
   put n=;
run;

Sure:

 

1) The Put Function returns the date value in a character string specified by the format ddmmyyn8 ''28102019"

2: The Input Function reads the character string from above and returns it as the number  28102019 using the 8. informat.

PeterClemmensen
Tourmaline | Level 20

Again, change the format..

 

data _null_;
   SASDate='28OCT2019'd;
   n=input(put(SASDate, yymmn4.), 8.);
   put n=;
run;

Also, I very much agree with @PaigeMiller 

Ronein
Meteorite | Level 14

Should it be 8. or 4. ???

PaigeMiller
Diamond | Level 26

While @PeterClemmensen has provided the answer, there is really no value in SAS to have a numeric 20191028. You can't do anything with this numeric value (except things that would be harder to do than if you left it as ’28OCT2019’d). SAS has built lots of functions to handle ’28OCT2019’d, but if you want to do anything with a numeric 20191028, you have to create your own functions.

 

Now, if you want a character 20191028 for specific output needs, that's understandable and easy to produce; but a numeric 20191028 is just making your future coding harder.

--
Paige Miller
Ronein
Meteorite | Level 14

I will summarize the examples of converting SAS date to numeric number:

 

Example 1:
SAS_Date=’28OCT2019’d;
Desired numeric is 20191028
Number_date= input(put(SAS_Date,yymmddn8.),8.);
Put Function returns the date value in a character specified by the format yymmddn8.''20191028"
Input Function reads the character from above and returns it as the number  20191028 using the 8. informat
Example 2:
SAS_Date=’28OCT2019’d;
Desired numeric is 28102019
Number_date= input(put(SAS_Date,DDMMYYn8.),8.);
Put Function returns the date value in a character specified by the format DDMMYYn8. ,''28102019"
Input Function reads the character from above and returns it as the number  28102019  using the 8. informat
Example 3:
SAS_Date=’28OCT2019’d;
Desired numeric is 1910
Number_date= input(put(SAS_Date,YYMMN4.),4.);
Put Function returns the date value in a character specified by the format YYMMn4. ,''1910"
Input Function reads the character from above and returns it as the number  1910  using the 4. informat

 

 

ballardw
Super User

And I see no advantage to having multiple non-date values.

 

With the date value I USE the value in any of those formats by using the appropriate format for grouping for reporting, analysis or graphing purposes. Showing the ability to create additional numeric variables does not show the utility of having that multiplicity of variables. For graphing they are BAD as the interval between tick marks by default is going to be way off. Graph something with an x-axis that uses your "numeric" date values similar to 20100320 that crosses a couple of year boundaries and see just how bat that is. For analysis such as a time-series they won't work at all as I expect SAS procs like Arima to throw errors about improper intervals. For regression where the values should have the same interval between 20101231 and 20110101 and 20110102 they won't.  I also believed that in the past you have asked a lot of questions dealing with numeric "dates" of the yymm version. And a great deal of the solutions started with creating actual SAS date values to allow calculation of intervals or comparing offset dates.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 37080 views
  • 3 likes
  • 4 in conversation