BookmarkSubscribeRSS Feed
DC2-CJH
Calcite | Level 5

Hi all I am trying to import the latest created file by the data at the end of it's name form a location. I have the below code that I am sure used to work but I am now getting an error (also provided below), can anyone assist and tell me what's wrong with the Macro as I can't work it out.

 

%LET location = \\Drive and file location entered here\file name here;

 

Code being run

 

%MACRO get_filenames(location);

filename _dir "%bquote(&location.)";

 

data filenames(keep=memname SeqNo Extention );

Excel_Dir=dopen( "_dir" );

if Excel_Dir > 0 then

 

do;

count=dnum(Excel_Dir);

do i=1 to count;

 

memname=dread(Excel_Dir,i);

SeqNo =i;

Extention = substr(memname,index(memname,".")+1,length(memname));

if Extention = "txt" then

Extention = "txt";

 

output filenames;

end;

end;

rc=dclose(Excel_Dir);

call symput("MaxID",SeqNo);

call symput("Extention",Extention);

 

run;

%DO j = 1 %TO &MaxID.;

 

data _null_;

set work.filenames;

where SeqNo = &j.;

call symput("inputfile",memname);

call symput("Position",(length(memname)-(length(Extention)+1)));

 

run;

%END;

%MEND get_filenames;

 

/* Create a stamp (set as todays date) */

DATA _null_;

FORMAT Stamp $10.;

Stamp = put(today(),date9.);

CALL symput('Stamp',Stamp);

RUN;

%PUT &Stamp;

%get_filenames(%bquote("&location."));

 

/* Select the name of the latest file by the max SeqNo */

PROC SQL NOPRINT;

SELECT compress(memname)

INTO: fileName

FROM filenames

WHERE SeqNo IN (SELECT max(SeqNo) FROM filenames)

;QUIT;

%PUT &fileName. ;

 

Error I am receiving

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

&MaxID.

ERROR: The %TO value of the %DO J loop is invalid.

ERROR: The macro GET_FILENAMES will stop executing.

 

Any idea what could be wrong with the above?

 

Thanks

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

It looks like maxid never gets populated.

so the preceding data step before probably doesn't run as expected.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 627 views
  • 0 likes
  • 2 in conversation