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 create SAS date from numeric date.

In the code I get null value. why?

/*create sas date from numeric date */
Data a;
x=20181029;
Run;

data b;
set a;
yy=substr(put(x,8.),3,2)*1;
mm=substr(put(x,8.),7,2)*1;
dd=substr(put(x,8.),5,2)*1;
new_x = mdy(mm, dd, yy); 
run;
1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

Year starts from 1 and takes 4 positions

Month starts from 5th taking 2 positions

day starts from 7th taking 2 positions

 

So, modify your substr() function. You will get the right answer.

data b;
set a;
yy=substr(put(x,8.),1,4)*1;
mm=substr(put(x,8.),5,2)*1;
dd=substr(put(x,8.),7,2)*1;
new_x = mdy(mm, dd, yy); 
put new_x = date10.;
run;

 

View solution in original post

7 REPLIES 7
andreas_lds
Jade | Level 19
Why so complicated? Just use something like new_x = input (cats(x), yymmdd10.))
Ronein
Meteorite | Level 14

What is the purpose of using cats function in this case?

andreas_lds
Jade | Level 19

@Ronein wrote:

What is the purpose of using cats function in this case?


The cats-functions helps avoiding the note (or error) complaining about automatic type conversion.

SASKiwi
PROC Star

Year starts at position 1, month at position 5, day at position 7.

KachiM
Rhodochrosite | Level 12

Year starts from 1 and takes 4 positions

Month starts from 5th taking 2 positions

day starts from 7th taking 2 positions

 

So, modify your substr() function. You will get the right answer.

data b;
set a;
yy=substr(put(x,8.),1,4)*1;
mm=substr(put(x,8.),5,2)*1;
dd=substr(put(x,8.),7,2)*1;
new_x = mdy(mm, dd, yy); 
put new_x = date10.;
run;

 

Kurt_Bremser
Super User

Why so complicated?

Do it in one statement:

Data a;
x=20181029;
Run;

data b;
set a;
new_x = input(put(x,z8.),yymmdd8.);
format new_x yymmddd10.;
put new_x=;
run;

Log excerpt:

28         data b;
29         set a;
30         new_x = input(put(x,z8.),yymmdd8.);
31         format new_x yymmddd10.;
32         put new_x=;
33         run;

new_x=2018-10-29
ballardw
Super User

Specifically why: the error in the value you supplied to MDY function:

1860  data b;
1861  set a;
1862  yy=substr(put(x,8.),3,2)*1;
1863  mm=substr(put(x,8.),7,2)*1;
1864  dd=substr(put(x,8.),5,2)*1;
1865  new_x = mdy(mm, dd, yy);
1866  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      1862:4   1863:4   1864:4
NOTE: Invalid argument to function MDY(29,10,18) at line 1865 column 9.
x=20181029 yy=18 mm=29 dd=10 new_x=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the
      operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 1865:9

MONTH must be 1 to 12. Day values acceptable will depend on the year and month. Getting value of 29 for MM should have been a clue that the date string was parsed incorrectly.

 

While you may get the correct date with a two digit year why bother to go to the work to ignore the provided full 4 digit year????

And as pointed out, a proper informat may be a better choice.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 7 replies
  • 1570 views
  • 3 likes
  • 6 in conversation