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;
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;
What is the purpose of using cats function in this case?
@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.
Year starts at position 1, month at position 5, day at position 7.
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;
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
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.