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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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