<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: import if exits in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750183#M235902</link>
    <description>&lt;P&gt;I have modified your code for %IMPORT_ONE so we can all see what is happening (and because I don't have your data sets).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date=20JUN2021; 
%let end_date=%sysfunc(today(),date9.);

%macro import_one(date);
    %put &amp;amp;=date;
%mend;

data _null_;
    date="&amp;amp;start_date"d;
    YYYYMMDD=put(date,yymmddn8.);
    do while (date&amp;lt;="&amp;amp;end_date"d);  
        date=intnx('day', date, 1, 's');    
        YYYYMMDD=put(date,yymmddn8.);
        call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This produces in the LOG:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    + %import_one(20210621)
DATE=20210621
2    + %import_one(20210622)
DATE=20210622
3    + %import_one(20210623)
DATE=20210623
4    + %import_one(20210624)
DATE=20210624
5    + %import_one(20210625)
DATE=20210625
&lt;/PRE&gt;
&lt;P&gt;So, the data step that produces CALL EXECUTE statements is not producing the desired set of calls to %IMPORT_ONE. Why? Because you have placed CALL EXECUTE after the date is incremented by 1. So the first date it produces in 20210621 and also it produces 20210625 (which is tomorrow).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you move CALL EXECUTE before you increment the date by 1, then you get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    date="&amp;amp;start_date"d;
    YYYYMMDD=put(date,yymmddn8.);
    do while (date&amp;lt;="&amp;amp;end_date"d);  
        call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
        date=intnx('day', date, 1, 's');    
        YYYYMMDD=put(date,yymmddn8.);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This type of debugging, which allows you to LOOK AT what is happening, is something you should be doing yourself. It's not clear if you are doing this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please, in the future, make your coding and debugging easier (and also make it easier for all of us) by indenting your code, as I have done.&lt;/P&gt;</description>
    <pubDate>Thu, 24 Jun 2021 12:59:37 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-06-24T12:59:37Z</dc:date>
    <item>
      <title>import if exits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750177#M235898</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to import txt files if they exists between 20JUN2021 till today (24JUN2021).&lt;/P&gt;
&lt;P&gt;Then I want to put them in stack structure (SET them&amp;nbsp; together).&lt;/P&gt;
&lt;P&gt;I see that actually the files that exists are :20JUN2021,21JUN2021, 22JUN2021,23JUN2021.&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;In the output data set I dont see data from 20JUN2021.&lt;/P&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;What is the problem in the code that data of 20JUN2021&amp;nbsp; is not imported?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date=20JUN2021; 
%let end_date=%sysfunc(today(),date9.);


%macro import_one(YYYYMMDD);
%if %sysfunc(fileexist(/path/ttt&amp;amp;YYYYMMDD..txt))
%then %do;
data DailyPanel_a;
infile "/path/ttt&amp;amp;YYYYMMDD."
dlm=';' dsd truncover firstobs=2;
input VAR1-VAR59;
informat VAR1-VAR59 best32. ;
format VAR1-VAR59 best12. ;
run;
%end;
%else %do;
data DailyPanel_a;
informat VAR1-VAR59 best32. ;
format VAR1-VAR59 best12. ;
stop;
run;
%end;
proc append data=DailyPanel_aa base=Joe.DailyPanel_All  force; run; quit;
%mend import_one;


data datestbl(keep=YYYYMMDD);
date="&amp;amp;start_date"d;
YYYYMMDD=put(date,yymmddn8.);
do while (date&amp;lt;="&amp;amp;end_date"d);  
output;  
date=intnx('day', date, 1, 's');    
YYYYMMDD=put(date,yymmddn8.);
call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jun 2021 12:26:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750177#M235898</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-06-24T12:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: import if exits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750181#M235901</link>
      <description>&lt;P&gt;How are you testing if the data to that particular date is in the file?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you should include the date as a variable in the dataset you are generating from the text file.&lt;/P&gt;
&lt;P&gt;So here is your %IMPORT_ONE() macro cleaned up a little.&amp;nbsp; It will add a variable DATE so you know which file the observations came from.&amp;nbsp; If gets rid of the useless INFORMAT and FORMAT statements. It also always makes the temporary dataset and always runs the PROC APPEND.&amp;nbsp; But when the file does not exist it makes an empty daily dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro import_one(YYYYMMDD);
data DailyPanel_a;
  length date var1-var59 8;
  date = input("&amp;amp;yyyymmdd",yymmdd8.);
  format date date9.;
%if %sysfunc(fileexist(/path/ttt&amp;amp;YYYYMMDD..txt)) %then %do;
  infile "/path/ttt&amp;amp;YYYYMMDD." dlm=';' dsd truncover firstobs=2;
  input VAR1-VAR59;
%end;
%else %do;
  stop;
%end;
run;
%end;
proc append data=DailyPanel_aa base=Joe.DailyPanel_All force; 
run; 
%mend import_one;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's also simplify the data step that is generating one macro call per date:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datestbl;
  do date="&amp;amp;start_date"d to "&amp;amp;end_date"d;
    call execute(cats('%nrstr(%import_one)(',put(date,yymmddn8.),')'));
    output;
  end;
  format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want there to be a record in the final aggregate table when the text file does not exist for that date you can remove the STOP statement in the macro.&amp;nbsp; But if the file exists but it has zero observations you will still not get any records for that date.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jun 2021 12:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750181#M235901</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-24T12:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: import if exits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750183#M235902</link>
      <description>&lt;P&gt;I have modified your code for %IMPORT_ONE so we can all see what is happening (and because I don't have your data sets).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date=20JUN2021; 
%let end_date=%sysfunc(today(),date9.);

%macro import_one(date);
    %put &amp;amp;=date;
%mend;

data _null_;
    date="&amp;amp;start_date"d;
    YYYYMMDD=put(date,yymmddn8.);
    do while (date&amp;lt;="&amp;amp;end_date"d);  
        date=intnx('day', date, 1, 's');    
        YYYYMMDD=put(date,yymmddn8.);
        call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This produces in the LOG:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    + %import_one(20210621)
DATE=20210621
2    + %import_one(20210622)
DATE=20210622
3    + %import_one(20210623)
DATE=20210623
4    + %import_one(20210624)
DATE=20210624
5    + %import_one(20210625)
DATE=20210625
&lt;/PRE&gt;
&lt;P&gt;So, the data step that produces CALL EXECUTE statements is not producing the desired set of calls to %IMPORT_ONE. Why? Because you have placed CALL EXECUTE after the date is incremented by 1. So the first date it produces in 20210621 and also it produces 20210625 (which is tomorrow).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you move CALL EXECUTE before you increment the date by 1, then you get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    date="&amp;amp;start_date"d;
    YYYYMMDD=put(date,yymmddn8.);
    do while (date&amp;lt;="&amp;amp;end_date"d);  
        call execute('%nrstr(%import_one('!!YYYYMMDD!!'))');
        date=intnx('day', date, 1, 's');    
        YYYYMMDD=put(date,yymmddn8.);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This type of debugging, which allows you to LOOK AT what is happening, is something you should be doing yourself. It's not clear if you are doing this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please, in the future, make your coding and debugging easier (and also make it easier for all of us) by indenting your code, as I have done.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jun 2021 12:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-exits/m-p/750183#M235902</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-24T12:59:37Z</dc:date>
    </item>
  </channel>
</rss>

