I have this dataset and I there is a variable that is in the DATETIME format and I want it in the DATE9. format.
My code:
data a;
set casesdat.cases;
discharge = DatePart(Date_Discharged);
run;
data b;
set a;
format discharge date9.;
where '01jan2020'd <= discharge <= '20dec2020'd;
run;
The errors that come up:
I used a libname statement to read in the dataset and I double checked that the variables are in the right format with a proc contents.
You are getting these errors because variable DISCHARGE is character, it is not numeric, and so the formatting and WHERE statement will not work (these are expecting numeric values)
@Windata wrote:
You should specify the date9. format in the first data step - see below. Also, I think you can do the where clause in the same data step as well.
data a;
set casesdat.cases;
format discharge date9.;
discharge = DatePart(Date_Discharged);
where '01jan2020'd <= discharge <= '20dec2020'd;
run;
You cannot reference the derived variable in the WHERE clause. The where clause filters the data on the way into the data step, so it happens before DISCHARGE is created.
You could use a subsetting IF instead.
Or reference the original variable in the WHERE clause. Either using appropriate datetime literals or using the DATEPART() function.
if '01jan2020'd <= discharge <= '20dec2020'd;
where '01jan2020:00:00'dt <= Date_discharged < '21dec2020:00:00'dt;
where '01jan2020'd <= datepart(Date_discharged) <= '20dec2020'd;
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.