<?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: infile statement with macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786660#M251209</link>
    <description>&lt;P&gt;What is your question?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you show us what you have tried so far?&lt;/P&gt;</description>
    <pubDate>Sun, 19 Dec 2021 14:14:33 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-12-19T14:14:33Z</dc:date>
    <item>
      <title>infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786657#M251207</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running production job which needs to repeat every month by using SAS.&lt;/P&gt;&lt;P&gt;I have infile statement in my program which reads .dat datatype.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following infile statement, I do need to change 2021-12 (YYYY-MM) and 202112 (YYYYMM) every time I run the program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT color="#008000"&gt;INFILE 'C:\Users\John\Desktop\Files\&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;2021-12&lt;/FONT&gt;&lt;/STRONG&gt;-xx\SAS\SAS_C_&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;202112&lt;/STRONG&gt;.&lt;/FONT&gt;dat' LRECL=413&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#008000"&gt;ENCODING="utf-8"&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do need to add a macro to change this two fields automatically according to current year and month.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 14:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786657#M251207</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-19T14:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786660#M251209</link>
      <description>&lt;P&gt;What is your question?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you show us what you have tried so far?&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 14:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786660#M251209</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-19T14:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786669#M251210</link>
      <description>&lt;P&gt;&lt;FONT color="#000000"&gt;Hello,&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I tried following:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;%let var1 = 2021-12;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;%let var2 = 202112;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;INFILE 'C:\Users\John\Desktop\Files\&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;&amp;amp;var1&lt;/FONT&gt;&lt;/STRONG&gt;-xx\SAS\SAS_C_&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;&amp;amp;var2&lt;/STRONG&gt;.&lt;/FONT&gt;dat' LRECL=413&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#008000"&gt;ENCODING="utf-8"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I need to identify month and year by using today() or date() function to make program run automatically.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I want something like this in macro variable output:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;data test1;&lt;BR /&gt;enddate = (today());&lt;BR /&gt;enddate1 = (today());&lt;BR /&gt;format enddate yymmd7. enddate1 yymmn6.;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 14:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786669#M251210</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-19T14:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786670#M251211</link>
      <description>&lt;P&gt;That's pretty good. You need to use double quotes (not single quotes) in your INFILE statement to get that part to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So here's how I would do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    call symputx('today1',put(today(),yymmd7.));
    call symputx('today2',put(today(),yymmn6.));
run;
%put &amp;amp;=today1;
%put &amp;amp;=today2;

infile "C:\Users\John\Desktop\Files\&amp;amp;today1\SAS\SAS_C_&amp;amp;today2..dat" LRECL=413 ENCODING="utf-8";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the double quotes in the INFILE statement, it won’t work with single quotes around the file name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, note that I have created two macro variables (do not call them macros, they are not macros, they are macro variables, macros are different than macro variables), &amp;amp;TODAY1 which contains the value 2021-12 and &amp;amp;TODAY2 which contains the value 202112. I use a DATA step and CALL SYMPUTX to create these macro variables (you could also do this with a %LET statement, but then you also have to use %SYSFUNC() twice), and then I use a %PUT statement to make sure they have the desired values, which will change each month. The %PUT statements are optional, everything works without them. Lastly, there must be two dots after &amp;amp;today2 in the code, the first dot indicating the end of the macro variable’s name, and the second dot indicating an actual dot as part of the file name.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 21:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786670#M251211</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-19T21:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786702#M251220</link>
      <description>&lt;P&gt;Another possibility is to use the FILEVAR option on the INFILE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  fnam=cats( 'C:\Users\John\Desktop\Files\',put(today(),yymmdd10.),'\SAS\SAS_C_',put(today(),yymmn6.),'.dat');
  INFILE dummy filevar=fnam LRECL=413ENCODING="utf-8" end=done;
  do while(not done);
    input &amp;lt;whatever you want to input&amp;gt;;
    output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;- I assume the "-xx" meant that you want to have the current date (or some other date) in the filename.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to read more that one file, that is easily accomplished, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  format filedate yymmdd10.;
  do filedate=intnx('month',today(),0) to today();
    fnam=cats( 'C:\Users\John\Desktop\Files\',put(filedate,yymmdd10.),'\SAS\SAS_C_',put(filedate,yymmn6.),'.dat');
    INFILE dummy filevar=fnam LRECL=413ENCODING="utf-8" end=done;
    if fileexist(fnam) then do while(not done);
      input &amp;lt;whatever you want to input&amp;gt;;
      output;
      end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I put in a FILEEXIST call to check if the infile exists.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Dec 2021 09:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786702#M251220</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-12-20T09:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786709#M251225</link>
      <description>&lt;P&gt;Is there a way for us to select previous month automatically by using similar structure?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following infile statement, I do need to change 2021-11 (YYYY-MM) and 202111 (YYYYMM) every time I run the program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;INFILE 'C:\Users\John\Desktop\Files\&lt;STRONG&gt;2021-11&lt;/STRONG&gt;-xx\SAS\SAS_C_&lt;STRONG&gt;202111&lt;/STRONG&gt;.dat' LRECL=413&lt;BR /&gt;ENCODING="utf-8"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do need to add a macro variable to change this two fields automatically accordingly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am expecting to run this script in next month (Jan 2022). I do need macro variable that read&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;2021-12 and&amp;nbsp;202112.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*derive last month and year in yyyy-mm and yyyymm format. ;
*;
*need to convert this to sas macro variable;

*to use in infile statement;

data _null_;
   lastmonth = intnx('month',today(),-1,'E');
   put lastmonth= yymmd8.;
   lastmonth1 = intnx('month',today(),-1,'E');
   put lastmonth1= yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Dec 2021 11:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786709#M251225</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-20T11:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786713#M251227</link>
      <description>&lt;P&gt;If "-xx" is literal, and not a placeholder for the date, you can do it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  Last_month=intnx('month',today(),-1);
  fnam=cats( 'C:\Users\John\Desktop\Files\',put(Last_month,yymmd7.),'-xx\SAS\SAS_C_',put(Last_month,yymmn6.),'.dat');
  INFILE dummy filevar=fnam LRECL=413ENCODING="utf-8" end=done;
  do while(not done);
    input &amp;lt;whatever you want to input&amp;gt;;
    output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you want to loop through all the dates for the last month:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  format filedate yymmdd10.;
  do filedate=intnx('month',today(),-1) to intnx('month',today(),-1,'E');
    fnam=cats( 'C:\Users\John\Desktop\Files\',put(filedate,yymmdd10.),'\SAS\SAS_C_',put(filedate,yymmn6.),'.dat');
    INFILE dummy filevar=fnam LRECL=413ENCODING="utf-8" end=done;
    if fileexist(fnam) then do while(not done);
      input &amp;lt;whatever you want to input&amp;gt;;
      output;
      end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The 'E' or 'END' parameter to INTNX means to take the end of the period, if the parameter is omitted, it uses the beginning as default.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Dec 2021 11:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786713#M251227</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-12-20T11:10:20Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786714#M251228</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way for us to select previous month automatically by using similar structure?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following infile statement, I do need to change 2021-11 (YYYY-MM) and 202111 (YYYYMM) every time I run the program.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT color="#008000"&gt;INFILE 'C:\Users\John\Desktop\Files\&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;2021-11&lt;/FONT&gt;&lt;/STRONG&gt;-xx\SAS\SAS_C_&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;202111&lt;/STRONG&gt;.&lt;/FONT&gt;dat' LRECL=413&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#008000"&gt;ENCODING="utf-8"&lt;/FONT&gt;&lt;/P&gt;
&lt;P class="lia-indent-padding-left-30px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do need to add a macro variable to change this two fields automatically accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am expecting to run this script in next month (Jan 2022). I do need macro variable that read&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;2021-12 and&amp;nbsp;202112.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Its always helpful to state the real problem in your original message, that way we don't waste time producing solutions that don't work for the real problem you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change the original solution I gave to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    call symputx('today1',put(intnx('month',today(),-1,'b'),yymmd7.));
    call symputx('today2',put(intnx('month',today(),-1,'b'),yymmn6.));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Dec 2021 11:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786714#M251228</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-20T11:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: infile statement with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786716#M251229</link>
      <description>&lt;P&gt;Thank you it works.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Dec 2021 11:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/infile-statement-with-macro/m-p/786716#M251229</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-20T11:20:03Z</dc:date>
    </item>
  </channel>
</rss>

