I have a Date column that is numeric with format MMDDYY8. I want to modify one of the dates in my dataset. I tried the following code to change the date for a specific ID participant:
data want;
set have;
if IDvar eq "ABCD" then datevar = 03/08/20;
run;
Type | Ln | Format |
Num | 8 | MMDDYY8. |
but instead of the date I want, I got 01/01/60
01/01/60 |
May you please help me solve this?
Thanks!
You are close. Here is the way to do this:
if IDvar eq "ABCD" then datevar = '03AUG2020'd;
It has to be that exact syntax, except that it can be upper or lower or mixed case.
3 divided by 8 divided by 20 is 0.01875 which is less than 1. So 01JAN1960 is the right date for the value you stored.
Did you intend to set the date to the 08MAR2020? Or perhaps 08MAR1920?
If so then you can either use a date literal (a quoted string that the DATE informat can read followed by the letter d) or a function that returns that date or the actual date value of 21982.
datevar = '08mar2020'd;
datevar = "8MAR2020"d;
datevar = '01-mar-2020'd;
datevar = mdy(3,8,2020);
datevar = 21982 ;
Thank you, Reeza, PaigeMiller, and Tom. All your answers were very helpful and I learned from each response.
Thanks!
Hello @Emma_at_SAS
I'm glad you got an answer you can use.
If can make a suggestion, from now on you'd be better off using 4 digit years, rather than 2 digit years. This will avoid complications and confusion at some point in your programming career.
@Emma_at_SAS wrote:
Tom, may I ask how you calculated 21982?
Thanks!
Just ask SAS to show you.
208 data test; 209 x=mdy(3,8,20); 210 put x=; 211 run; x=21982
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.