I need to create a point in time variable in sas 9.3 using today's date where the date format in my data is datetime20.
I would like "today" to eq today's date.
Data WORK.want;
set WORK.want;
If A_Date > today then pt=0;
If B_Date=" " then pt=1;
If B_Date < today then pt=0;
If A_Date < today and B_Date => today then pt=1;
else pt=0;
Run;
Thanks!
@Whitlea wrote:
I need to create a point in time variable in sas 9.3 using today's date where the date format in my data is datetime20.
I would like "today" to eq today's date.
You would like TODAY to be equal to today's date, but your data is in format datetime20. This is a problem. It won't work the way you describe it. You can't directly compare a date to a datetime value. Instead, you want TODAY to be a datetime variable, most likely equal to 24APR2023:00:00:00. You can use the DATETIME() function and now you are comparing datetimes to datetimes, that will work. So the code would look like:
Data WORK.want1;
set WORK.want;
today=datetime(); /* Now today is a datetime variable */
if A_Date > today then pt=0;
if B_Date=" " then pt=1; /* This won't work if b_date is a numeric datetime variable, are you sure it is a numeric datetime variable? */
If B_Date < today then pt=0;
If A_Date < today and B_Date => today then pt=1;
else pt=0;
run;
As an alternative, you can do this which will compare dates to dates (using the DATEPART function to turn datetimes into dates):
Data WORK.want1;
set WORK.want;
today=today(); /* Now today is a date variable */
/* You can't directly compare a datetime variable like A_DATE to a DATE Variable */
/* But the datepart function turns a datetime variable into a date variable which can be compared to a date variable */
If datepart(A_Date) > today then pt=0;
If datepart(B_Date)=" " then pt=1; /* This won't work if b_date is a numeric datetime variable, are you sure it is a numeric datetime variable? */
If datepart(B_Date) < today then pt=0;
If datepart(A_Date) < today and datepart(B_Date) => today then pt=1;
else pt=0;
run;
@Whitlea wrote:
I need to create a point in time variable in sas 9.3 using today's date where the date format in my data is datetime20.
I would like "today" to eq today's date.
You would like TODAY to be equal to today's date, but your data is in format datetime20. This is a problem. It won't work the way you describe it. You can't directly compare a date to a datetime value. Instead, you want TODAY to be a datetime variable, most likely equal to 24APR2023:00:00:00. You can use the DATETIME() function and now you are comparing datetimes to datetimes, that will work. So the code would look like:
Data WORK.want1;
set WORK.want;
today=datetime(); /* Now today is a datetime variable */
if A_Date > today then pt=0;
if B_Date=" " then pt=1; /* This won't work if b_date is a numeric datetime variable, are you sure it is a numeric datetime variable? */
If B_Date < today then pt=0;
If A_Date < today and B_Date => today then pt=1;
else pt=0;
run;
As an alternative, you can do this which will compare dates to dates (using the DATEPART function to turn datetimes into dates):
Data WORK.want1;
set WORK.want;
today=today(); /* Now today is a date variable */
/* You can't directly compare a datetime variable like A_DATE to a DATE Variable */
/* But the datepart function turns a datetime variable into a date variable which can be compared to a date variable */
If datepart(A_Date) > today then pt=0;
If datepart(B_Date)=" " then pt=1; /* This won't work if b_date is a numeric datetime variable, are you sure it is a numeric datetime variable? */
If datepart(B_Date) < today then pt=0;
If datepart(A_Date) < today and datepart(B_Date) => today then pt=1;
else pt=0;
run;
Thank you!
How would I do this if B_Date was in format datetime20. as well?
@Whitlea wrote:
Thank you!
How would I do this if B_Date was in format datetime20. as well?
You would use
if b_date=. then ... ;
since a numeric variable missing value is indicated by a dot and not by " "
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.