BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;


 

 

 

1 REPLY 1
Rick_SAS
SAS Super FREQ

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 524 views
  • 0 likes
  • 2 in conversation