BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shl007
Obsidian | Level 7

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);
1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
r_behata
Barite | Level 11
%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?

 

Kurt_Bremser
Super User

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;
shl007
Obsidian | Level 7

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)

Shmuel
Garnet | Level 18

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;
shl007
Obsidian | Level 7

Yep I had something similar to that. I had to tweak something else, and it worked. Thanks everyone!

Tom
Super User Tom
Super User

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;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 801 views
  • 0 likes
  • 5 in conversation