BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to import if file exists and if it doesn't exists then I want to create an empty data set.

What is wrong in my code?

How can I solve it please?

 

%let list=20120404+20210417+20210319;
%let n=3;

%macro ttt; 
%do j=1 %to &n.;
%let YYYYMMDD=%scan(&List.,&j.,+);
%if %sysfunc(fileexist("/path/A&YYYYMMDD."))
%then %do;   
DATA t&YYYYMMDD.;
INFILE "/path/A&YYYYMMDD."
firstobs=2;
INPUT 
@1  X1 8. 
@10 X2 2. 
@13 X3 3. 
@17 X4 9. 
;
RUN;

%else %do;
Data t&YYYYMMDD.;
X1=.;
X2=.;
X3=.;
X4=.;
Run;


%end;
%end;
%mend ttt;
%ttt; 
3 REPLIES 3
sbxkoenk
SAS Super FREQ

Hello,

If you claim that something does not work. Then please post the LOG. That will facilitate things for everyone!

 

Anyway, I do not think anything is wrong with the the FILEEXIST function, but there's something wrong with the macro definition.

There are 3 %DO's but only 2 %END's.

Therefore, I get this error when I run your code:

ERROR: There is no matching %IF statement for the %ELSE.
ERROR: A dummy macro will be compiled.

 

Cheers,

Koen

Patrick
Opal | Level 21

Based on what you've posted below code that should work for you.

options mprint mlogic;
%let list=20120404 20210417 20210319;
%let path=c:\\temp;

%macro ttt;
  %local path_delim;

  %if %sysfunc(findc(%nrbquote(%path),%nrbquote(\))) %then
    %let path_delim=\;
  %else %let path_delim=/;
  %local n;
  %let n=%sysfunc(countw(&list));

  %do j=1 %to &n.;
    %let yyyymmdd=%scan(&list.,&j.);
    %put &=yyyymmdd;

    %if %sysfunc(fileexist("&path.&path_delim.A&yyyymmdd..txt")) %then
      %do;
        data t&yyyymmdd.;
          infile "&path.&path_delim.A&yyyymmdd..txt" firstobs=2 truncover;
          input 
            @1  x1 :8. 
            @10 x2 :2. 
            @13 x3 :3. 
            @17 x4 :9. 
          ;
        run;
      %end;
    %else
      %do;
        data t&yyyymmdd.;
          stop;
          length x1 x2 x3 x4 8;
        run;
      %end;
  %end;
%mend ttt;

%ttt;

For the cases where you want to write an empty data step use a STOP statement as else the data step will first go through the compile phase where it creates the table structure (which is what you want) but then also goes trough the first iteration of the execution phase and will write an empty row to the table (which you don't want). Using a STOP statement prevents this from happening. 

The execution phase will still start but end as soon as it hits the stop statement - and as it never reaches the RUN statement there won't be an implicit OUTPUT (writing the empty row).

You could position the STOP statement anywhere in the data step. I prefer to have it at the beginning for such cases to make it clear in the code that we only want the result from the compilation phase (all commands like keep, put, length, attrib and the like take effect in the compilation phase and though can come after the STOP statement that only comes into play during the execution phase).

Tom
Super User Tom
Super User

It might be clearer to put the %IF/%THEN inside the data step.

%macro ttt; 
  %local j yyyymmdd ;
  %do j=1 %to %sysfunc(countw(&list.,+);
    %let YYYYMMDD=%scan(&List.,&j.,+);
DATA t&YYYYMMDD.;
  LENGTH X1-X4 8 ;
  %if %sysfunc(fileexist("/path/A&YYYYMMDD.")) %then %do;   
  INFILE "/path/A&YYYYMMDD." firstobs=2 truncover ;
  INPUT 
    @1  X1 8. 
    @10 X2 2. 
    @13 X3 3. 
    @17 X4 9. 
  ;
  %end;
  %else %do;
  STOP;
  %end;
RUN;
%mend ttt;

%let list=20120404+20210417+20210319;
%ttt;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2415 views
  • 0 likes
  • 4 in conversation