Hello
I am trying to write both Where and Keep statements.
Firstly where should be done and then keep.
I receive an error.
What is the way to solve it?
ERROR: Variable date is not on file WORK.WANT_MONTH.
%let start_date=31JAN2021;
%let end_date=14MAR2021;
data want_month(where=(date>"&start_date"d)keep=tbl_name);
date="&start_date"d;
do while (date<="&end_date"d);
output;
date=intnx('day', date, 1);
date_ddmmyyyy=put(date,ddmmyyn8.);
tbl_name=CATS("RRR.NewMethologyInput",date_ddmmyyyy);
end;
format date date9.;
run;
Here's a useful tip: The data set options (DROP, KEEP, RENAME, WHERE,...) are processed in alphabetical order. Therefore the KEEP statement is processed before the WHERE statement.
The best way to solve this problem is to use the WHERE statement (or a subsetting IF statement) in the body of the program and use the KEEP option for the output data set:
data want_month(keep=tbl_name);
date="&start_date"d;
do while (date<="&end_date"d);
output;
date=intnx('day', date, 1);
date_ddmmyyyy=put(date,ddmmyyn8.);
tbl_name=CATS("RRR.NewMethologyInput",date_ddmmyyyy);
end;
format date date9.;
if (date>"&start_date"d); /* could also use a WHERE clause here */
run;
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.