Hi,
I have a date variable that is numeric ((02JAN2018) ) with date9. format and date 9 informat length 8.
I would like it to be character date yymmdd10.
Your help will be appreciated
There is no such thing as a character date in SAS. You can convert the date to a plain old character string, which is only useful in labels and titles and reports. If you care going to do additional computations or work with the date, you need to leave it as numeric.
Here is the conversion to character:
char_date = put(date,yymmdd10.);
Perhaps you meant that you want to leave the variable as numeric but with a different format? That would be much more useful and reasonable than creating a character string. In that case, just change the format.
Editor's note:
As PaigeMiller noted, converting the date to a character string is different than reformatting the date with a format that uses characters. If reformatting is the goal, Astounding provides example code that can be used in a Proc step or a DATA step.
format datevar yymmdd10.;
There is no such thing as a character date in SAS. You can convert the date to a plain old character string, which is only useful in labels and titles and reports. If you care going to do additional computations or work with the date, you need to leave it as numeric.
Here is the conversion to character:
char_date = put(date,yymmdd10.);
Perhaps you meant that you want to leave the variable as numeric but with a different format? That would be much more useful and reasonable than creating a character string. In that case, just change the format.
Editor's note:
As PaigeMiller noted, converting the date to a character string is different than reformatting the date with a format that uses characters. If reformatting is the goal, Astounding provides example code that can be used in a Proc step or a DATA step.
format datevar yymmdd10.;
Thank you for your response, i would like to create a new character variable with yymmdd10. format.
Thank you! I just changed the format and it worked!
data test;
Date='02JAN2018'd;
CharDate=put(Date, yymmdd10.);
format Date date9.;
run;
However, not a good idea to have dates stored as character...
If you are not happy with the DATE9 format, just use a different format:
format datevar yymmdd10.;
You can add that statement in a PROC step, in which case the format is used just for the duration of the procedure.
Or you can add it in a DATA step, in which case the format becomes the default format going forward from that point.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.