<?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: How can I automate the macro to import multiple annual and quarterly files ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671879#M201819</link>
    <description>&lt;P&gt;CALL EXECUTE.&lt;/P&gt;
&lt;P&gt;Run this first. Verify that the str calculated looks like your macro execution lines below. If not adjust the code until it does. Once it's correct, uncomment the CALL EXECUTE line and it will execute the commands.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Blog describing usage&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Documentation reference&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1q1527d51eivsn1ob5hnz0yd1hx.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1q1527d51eivsn1ob5hnz0yd1hx.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data run_macros;

do year=2016 to 2019;
    do quarter=1 to 4;

          year2digit = substrn(year, 3, 2);
          month_name = put(mdy(quarter*3, 1, year), monname3.);
          str = catt('%YEARQTR(', year2digit, ', ', quarter, ', ', month_name, ');');
         *call execute(str);

    end;
end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can I better automate this code to import multiple files to create quarterly datasets as described below. Thank you!&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;%MACRO YEARQTR (YR,QTR,MON);

PROC IMPORT DATAFILE= "C:\Users\&amp;amp;SYSUSERID\&amp;amp;MON. &amp;amp;YR. ABC File.xlsx" 
						OUT= ABC_&amp;amp;YR._Q&amp;amp;QTR
						DBMS=xlsx REPLACE;
						GETNAMES=YES;
RUN;

%MEND;

%YEARQTR (19,1,Jan);
%YEARQTR (19,2,Apr);
%YEARQTR (19,3,Jul);
%YEARQTR (19,4,Oct);

%YEARQTR (18,1,Jan);
%YEARQTR (18,2,Apr);
%YEARQTR (18,3,Jul);
%YEARQTR (18,4,Oct);

%YEARQTR (17,1,Jan);
%YEARQTR (17,2,Apr);
%YEARQTR (17,3,Jul);
%YEARQTR (17,4,Oct);

%YEARQTR (16,1,Jan);
%YEARQTR (16,2,Apr);
%YEARQTR (16,3,Jul);
%YEARQTR (16,4,Oct);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jul 2020 16:35:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-07-23T16:35:31Z</dc:date>
    <item>
      <title>How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671873#M201815</link>
      <description>&lt;P&gt;How can I better automate this code to import multiple files to create quarterly datasets as described below. Thank you!&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;%MACRO YEARQTR (YR,QTR,MON);

PROC IMPORT DATAFILE= "C:\Users\&amp;amp;SYSUSERID\&amp;amp;MON. &amp;amp;YR. ABC File.xlsx" 
						OUT= ABC_&amp;amp;YR._Q&amp;amp;QTR
						DBMS=xlsx REPLACE;
						GETNAMES=YES;
RUN;

%MEND;

%YEARQTR (19,1,Jan);
%YEARQTR (19,2,Apr);
%YEARQTR (19,3,Jul);
%YEARQTR (19,4,Oct);

%YEARQTR (18,1,Jan);
%YEARQTR (18,2,Apr);
%YEARQTR (18,3,Jul);
%YEARQTR (18,4,Oct);

%YEARQTR (17,1,Jan);
%YEARQTR (17,2,Apr);
%YEARQTR (17,3,Jul);
%YEARQTR (17,4,Oct);

%YEARQTR (16,1,Jan);
%YEARQTR (16,2,Apr);
%YEARQTR (16,3,Jul);
%YEARQTR (16,4,Oct);&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jul 2020 16:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671873#M201815</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-07-23T16:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671879#M201819</link>
      <description>&lt;P&gt;CALL EXECUTE.&lt;/P&gt;
&lt;P&gt;Run this first. Verify that the str calculated looks like your macro execution lines below. If not adjust the code until it does. Once it's correct, uncomment the CALL EXECUTE line and it will execute the commands.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Blog describing usage&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Documentation reference&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1q1527d51eivsn1ob5hnz0yd1hx.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n1q1527d51eivsn1ob5hnz0yd1hx.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data run_macros;

do year=2016 to 2019;
    do quarter=1 to 4;

          year2digit = substrn(year, 3, 2);
          month_name = put(mdy(quarter*3, 1, year), monname3.);
          str = catt('%YEARQTR(', year2digit, ', ', quarter, ', ', month_name, ');');
         *call execute(str);

    end;
end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can I better automate this code to import multiple files to create quarterly datasets as described below. Thank you!&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;%MACRO YEARQTR (YR,QTR,MON);

PROC IMPORT DATAFILE= "C:\Users\&amp;amp;SYSUSERID\&amp;amp;MON. &amp;amp;YR. ABC File.xlsx" 
						OUT= ABC_&amp;amp;YR._Q&amp;amp;QTR
						DBMS=xlsx REPLACE;
						GETNAMES=YES;
RUN;

%MEND;

%YEARQTR (19,1,Jan);
%YEARQTR (19,2,Apr);
%YEARQTR (19,3,Jul);
%YEARQTR (19,4,Oct);

%YEARQTR (18,1,Jan);
%YEARQTR (18,2,Apr);
%YEARQTR (18,3,Jul);
%YEARQTR (18,4,Oct);

%YEARQTR (17,1,Jan);
%YEARQTR (17,2,Apr);
%YEARQTR (17,3,Jul);
%YEARQTR (17,4,Oct);

%YEARQTR (16,1,Jan);
%YEARQTR (16,2,Apr);
%YEARQTR (16,3,Jul);
%YEARQTR (16,4,Oct);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 16:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671879#M201819</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-23T16:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671887#M201824</link>
      <description>Thank you. By doing it this way in a dataset can I get rid of the repeating parameter lines ? I am looking for a way to create a loop or shorter macro for the year, months and quarters parameters.&lt;BR /&gt;</description>
      <pubDate>Thu, 23 Jul 2020 16:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671887#M201824</guid>
      <dc:creator>AP718</dc:creator>
      <dc:date>2020-07-23T16:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671891#M201826</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here's some example code. Basically I have created a macro to call your macro, you will need to replace my:&lt;BR /&gt;&amp;nbsp;&lt;CODE class=" language-sas"&gt;%put yearqtr(&amp;amp;year,&amp;amp;qtr,&amp;amp;mon) ; &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;With the call to your maco&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myload(startYear, endYear) ;
	proc format ;
		value myMonth 
			1="Jan"
			2="Apr" 
			3="Jul"
			4="Oct" ;
	run ;
	
	%do year=&amp;amp;startYear %to &amp;amp;endYear ;
		%do qtr=1 %to 4 ;
			%let mon=%sysfunc(putn(&amp;amp;qtr,myMonth.)) ;
			
			%put yearqtr(&amp;amp;year,&amp;amp;qtr,&amp;amp;mon) ;
		%end ;
	%end ;
	
%mend ;

%myload(15,18) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 17:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671891#M201826</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2020-07-23T17:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671894#M201828</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140206"&gt;@AP718&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you. By doing it this way in a dataset can I get rid of the repeating parameter lines ? I am looking for a way to create a loop or shorter macro for the year, months and quarters parameters.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You in effect build the needed code using the data set values to build the desired string holding the file names and output data set names.&lt;/P&gt;
&lt;P&gt;Similar approaches could use just years actual dates or lists of specific values in the do loops.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest that you add the SCANTEXT option to the import code to examine more rows of data before setting the lengths of character variables. The Proc Import procedure only uses the values of the first 20 rows by default to set lengths and if the first rows of file have a column that is not always used you could end up with a length of 1 character for the variable and lots of truncated data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Warning:&lt;/STRONG&gt; Unless your source of XLSX files is extremely consistent you may end up spending a lot of time fixing data imported using proc import because each separate call to Proc Import makes separate decisions based on the content of the file for data types, lengths and even formats, especially in the case of date/time/datetime values. So when you go to combine any of these sets you may get warnings of possibly truncated data from different length strings or errors because of incompatible data types.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 17:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671894#M201828</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-23T17:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671896#M201829</link>
      <description>Yes, it entirely gets rid of your macro loops. I find CALL EXECUTE simpler to work with and easier to debug in the long run, but that's very much a personal preference.</description>
      <pubDate>Thu, 23 Jul 2020 17:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671896#M201829</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-23T17:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can I automate the macro to import multiple annual and quarterly files ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671908#M201833</link>
      <description>&lt;P&gt;You can use INTNX() to increment by quarter.&amp;nbsp; &amp;nbsp;You need to provide a start date and an end date.&amp;nbsp;Let's call those STARTDT and ENDDT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let startdt='01JAN2016'd;
%let enddt='01OCT2019'd;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;These things are easier in data step than in macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  do offset=0 to intck('qtr',&amp;amp;startdt,&amp;amp;enddt);
    dt=intnx('qtr',&amp;amp;startdt,offset);
    yr=year(dt)-2000;
    qtr=qtr(dt);
    mon=put(dt,monname3.);
    call execute(cats('%nrstr(%yearqtr)(YR=',yr,',QTR=',qtr,'MON=',mon,')'));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you could do it in a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro qtr(startdt,enddt);
  %local offset dt yr qtr mon ;
  %do offset=0 %to %sysfunc(intck(qtr,&amp;amp;startdt,&amp;amp;enddt));
    %let dt=%sysfunc(intnx(qtr,&amp;amp;startdt,&amp;amp;offset));
    %let yr=%eval(%sysfunc(year(&amp;amp;dt)))-2000);
    %let qtr=%sysfunc(qtr(&amp;amp;dt));
    %let mon=%sysfunc(putn(&amp;amp;dt,monname3.));
    %yearqtr(YR=&amp;amp;yr,QTR=&amp;amp;qtr,MON=&amp;amp;mon)
  %end;
%mend;
%qtr(&amp;amp;startdt,&amp;amp;enddt);&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, 23 Jul 2020 17:56:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-automate-the-macro-to-import-multiple-annual-and/m-p/671908#M201833</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-23T17:56:07Z</dc:date>
    </item>
  </channel>
</rss>

