Hello there,
The current date format is yyyy-mm-dd, and proc contents shows the formate is $10, i want to change it into numeric format, but the following codes don't work. could anyone help me to find the reason?
data temp01; set temp01;
date1=input(date,$10.);
format date1 mmddyy8.;
run;
@Songchan At compile time, SAS creates and stores the metadata/descriptor portion of the variables. Therefore, at execution you will need a new variable to stored the converted values, i.e in your case the converted char date to num sas date. This essentially means a new assignment.
Therefore
numeric_sas_Date= input (char_date, yymmdd10.);
The above states, you want to create a numeric sas date by reading nonstandard character date using the appropriate informat which is yymmdd10. This is what Paigemiller demonstrated earlier. I chimed in only to help you understand.
@Songchan wrote:
The current date format is yyyy-mm-dd,
Okay, stop right there! It is yyyy-mm-dd, so don't convert using $10.
Use the appropriate informat that recognizes it as a calendar date
date1=input(date,yymmdd10.);
Hi @Songchan The way to think through intuitively is to understand the proc contents report. Since your proc contents report says the date is a character date, it is not stored as a number. When we talk of character date to be converted to a numeric date aka SAS date, we are basically dealing with something known as non standard data which could be a mix of chars/nums etc.
Typically we read non standard data with tools known as informats. Informats will read the number of bytes of values in the variable specified in it, and convert the value to a valid number in your case and therefore the requirement is to appropriate with a compatible informat which is yymmdd10. as opposed to character format $10. Should the informat fail to read the nonstandard data and convert appropriately, it triggers the automatic variable _ERROR_=1 and writes a note to the log and writes a missing value. Of course there are certain modifiers, that can used to suppress this behavior but that is out of scope for your rather straight forward requirement.
HTH & Regards!
input(date,yymmdd10.);
@Songchan wrote:
Hello there,
The current date format is yyyy-mm-dd, and proc contents shows the formate is $10, i want to change it into numeric format, but the following codes don't work. could anyone help me to find the reason?
data temp01; set temp01; date1=input(date,$10.); format date1 mmddyy8.; run;
Thank you for replying me, do you mean i can write like the following:
data temp01; set temp01;
input (date, yymmdd10.);
format date yymmddn8.;
run;
It still doesn't work.
@Songchan At compile time, SAS creates and stores the metadata/descriptor portion of the variables. Therefore, at execution you will need a new variable to stored the converted values, i.e in your case the converted char date to num sas date. This essentially means a new assignment.
Therefore
numeric_sas_Date= input (char_date, yymmdd10.);
The above states, you want to create a numeric sas date by reading nonstandard character date using the appropriate informat which is yymmdd10. This is what Paigemiller demonstrated earlier. I chimed in only to help you understand.
Thank you so much! That works!
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.