Hi - I'm using an INFILE statement in a macro, and the message I am getting is, ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
last
Where "last" is being invoked is here as part of the INFILE statement (END = last). Any ideas what I'm doing wrong? This is simplified code to test the usage of "last." Thanks
%macro countfil(dsn);
data &dsn; length fil2read $1000;
INFILE output TRUNCOVER ;
INPUT name $1000.;/*Pull pipe in*/
fil2read=name;
INFILE dummy FILEVAR=fil2read DSD END=last missover FIRSTOBS=9 LENGTH=len ;
%if last %then %do;
output;
%end;
run;
%mend countfil;
filename output pipe 'ls -a /mypath/*.csv' ;
%countfil(datasetname);
last is a data step variable and therefore not available while the macro preprocessor prepares program code.
In the data step, use Base SAS code:
if last then output;
%if last %then %do;
output;
The above logic does not seem to make much sense , are you trying to write the last observation of the by group to the output dataset?
last is a data step variable and therefore not available while the macro preprocessor prepares program code.
In the data step, use Base SAS code:
if last then output;
When I take out the %'s and use the normal non-macro language, the dataset is empty (even though I see the PIPE pulling in files)? Any ideas what could be going wrong? I don't have any filters or subsetting criteria other than the "last".
Eventually my goal is to output the total number of observations (in response to the last user's comment as to why this code is the way it is - I plan to add a counter once I prove "last" is working as a variable)
Try next code:
%macro countfil(dsn);
data &dsn; length fil2read $1000;
INFILE output TRUNCOVER ;
INPUT name $1000.;/*Pull pipe in*/
fil2read=name;
INFILE dummy FILEVAR=fil2read DSD
EOV=last missover FIRSTOBS=9 LENGTH=len ;
if last then output;
run;
%mend countfil;
Yep I had something similar to that. I had to tweak something else, and it worked. Thanks everyone!
You are trying to use macro logic where you want data step logic.
You have two INFILE statements.
Which one are you trying to count?
Also note you need to test the LAST variable BEFORE the INPUT statement if you want to handle empty input (or in this case input with less than 10 lines).
Get it to work WITHOUT macro first.
data &dsn;
INFILE output TRUNCOVER ;
INPUT name $1000.;
fil2read=name;
INFILE dummy FILEVAR=fil2read DSD END=last TRUNCOVER FIRSTOBS=9 LENGTH=len ;
do cnt=1 by 1 while ( not last );
input ;
end;
put name= cnt=;
run;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.