BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

You would be unable to run this code.

I am inputting a csv file name AL_MAA_mystate_FromState_20220211_02.csv.

I defined a numeric variable called code. It was designed to choose the appropriate number at the end of the _ (ie _01 or _02) as follows:

%let CODE = %str(01,02,03);
%If &filehere=Y %Then %DO;
        Data &ThisStateID._Data;
         
		infile "&myPath/&ThisStateID./&ThisStateRpt.&CURRDATE_KEY._&CODE..csv" 
            LRECL=32767  FIRSTOBS=4  ENCODING="LATIN1"   DLM=','    MISSOVER   DSD ;
         &INPUTLIST;
		 
       Run;
     %End;



Instead of getting the correct number extension I get an error message showing all 3 entries as follows
ERROR: Physical file does not exist, /myshare/mydrive0/al/AL_MAA_mystate_FromState_20220211_01,02,03.csv.

Is there a way to get the code to select the correct numeric extension instead. In this case it would be _02

4 REPLIES 4
Ksharp
Super User

Maybe you want this ?

 

%let CODE =%scan( %str(01,02,03) ,2,%str(,) );

%put &=CODE ;
Tom
Super User Tom
Super User

You don't appear to have defined any variables at all. 

You did put a string with commas in it into the macro variable named CODE.

 

Is there just one file you want to read into a dataset?  If so then how is the program supposed to know that you want to use the string 02 to build the filename? Instead of 01 or 03?  Do you want it to check and see which files exist?

 

Or is the goal to actually read three files?  If so do you want to read them into a single dataset or into three different datasets?

 

I am not sure how the filename you mention at the start of your question relates to the filename you are trying to build form macro variables in the code you showed.  

 

Please show example values of the other macro variables that the code is referencing.

ballardw
Super User

@Q1983 wrote:

You would be unable to run this code.

I am inputting a csv file name AL_MAA_mystate_FromState_20220211_02.csv.

I defined a numeric variable called code. It was designed to choose the appropriate number at the end of the _ (ie _01 or _02) as follows:

%let CODE = %str(01,02,03);
%If &filehere=Y %Then %DO;
        Data &ThisStateID._Data;
         
		infile "&myPath/&ThisStateID./&ThisStateRpt.&CURRDATE_KEY._&CODE..csv" 
            LRECL=32767  FIRSTOBS=4  ENCODING="LATIN1"   DLM=','    MISSOVER   DSD ;
         &INPUTLIST;
		 
       Run;
     %End;



Instead of getting the correct number extension I get an error message showing all 3 entries as follows
ERROR: Physical file does not exist, /myshare/mydrive0/al/AL_MAA_mystate_FromState_20220211_01,02,03.csv.

Is there a way to get the code to select the correct numeric extension instead. In this case it would be _02


Just what file name did you expect to create. I would guess that you might be attempting to do something with three separate files but the name you created does not match any of them.

 

You might want to look up the INFILE documentation for FILEVAR with special attention to the comments about FIRSTOBS in case you ever have files with just the header and no body.

andreas_lds
Jade | Level 19

Please post all relevant statement, including the statement setting "filehere".

An idea to fix this:

data _null_;
  length csvFile $ 200;
  do code = '01, '02', '03';
    csvFile = cats("&myPath/&ThisStateID/&ThisStateRpt.&Currdate_Key._", Code, ".csv";
    if fileexist(csvFile) then do;
      call symputx('csvFile', csvFile);
      call symputx('filehere', 'Y');
      leave;
    end;
  end;
run;

%if &filehere. = Y %then do;
  data ...;
    infile "&csvFile" ....;
    ...
  run;
%end;
%end;

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
  • 4 replies
  • 625 views
  • 4 likes
  • 5 in conversation