<?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: Loop values into macro parameters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629664#M186262</link>
    <description>Tom: How would I then pass those name values (i.e. NSDQsh201012.zip, NSDQsh201101.zip, ...) iteratively into the macro parameters (i.e. .source= , and outds=)?</description>
    <pubDate>Thu, 05 Mar 2020 00:19:19 GMT</pubDate>
    <dc:creator>AdamMadison</dc:creator>
    <dc:date>2020-03-05T00:19:19Z</dc:date>
    <item>
      <title>Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629633#M186241</link>
      <description>&lt;P&gt;It appears that my earlier post was identified as spam for some reason and deleted. So I am re-posting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The work process I have&amp;nbsp; in the following program takes a zip file (&lt;CODE class=" language-sas"&gt;NSDQsh201012.zip&lt;/CODE&gt;), processes it and outputs a data set (&lt;CODE class=" language-sas"&gt;nasdaq.NSDQsh201012).&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;I would like to go through this process for many different zip files which are named by "date" (Year/Month). For example: there is&amp;nbsp;&lt;CODE class=" language-sas"&gt;NSDQsh201012.zip,&amp;nbsp;NSDQsh201101.zip,&amp;nbsp;NSDQsh201102.zip, ....&amp;nbsp;NSDQsh202002.zip. &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;And the output datasets should be named accordingly. For example:&amp;nbsp;nasdaq.NSDQsh201012,&amp;nbsp;nasdaq.NSDQsh201101,&amp;nbsp;nasdaq.NSDQsh201102, ...&amp;nbsp;nasdaq.NSDQsh202002.&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;Ideally, I would construct a do loop that cycles through these "dates" (Year/Month) and changes the macro parameters on each iteration.&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;Something like:&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;%do year=2010 to 2020&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;%do month=01 to 12&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would have:&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;source=NSDQsh&amp;amp;year&amp;amp;month.zip.&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;But I am not sure how to accomplish this. Any assistance will be appreciated.&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* create dataset with members in specific zip file */
%zipMemList(source=NSDQsh201012.zip, outds=memlist);



/* execute macro %ReadMemInZip() once per member in zip file */
data _null_;
  set memlist;
  cmd=cats('%ReadMemInZip(source=',zip,', member=',memname,', outds=want)');
  call execute(cmd);
run;

%convertdaily(source=want, outds=nasdaq.NSDQsh201012);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629633#M186241</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-04T22:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629638#M186243</link>
      <description>&lt;P&gt;What's in a specific zip file? Just a single text file that's called the same name like the zip file but with extension .txt? Or something else?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629638#M186243</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-04T22:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629641#M186246</link>
      <description>Inside the zip files are numerous txt files. Different name than the zip (they are named NSDQsh&amp;amp;year&amp;amp;month&amp;amp;day).</description>
      <pubDate>Wed, 04 Mar 2020 22:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629641#M186246</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-04T22:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629642#M186247</link>
      <description>&lt;P&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank" rel="noopener"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What about changing your process?&lt;/P&gt;
&lt;P&gt;You're calling zipMemlist manually, why not loop within a data step and two call executes?? Change your program that reads members to a macro as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Small change to format the variable correctly to YYMMN.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
start_date = '01Jan2010'd;

do i=0 to 119;
date = intnx('month', start_date, i, 'b');
date_formatted = put(date, yymmn6.);

str1 = catt('%zipmemList(source=NSDQSH', date_formatted, ', outds=memlist);');
call execute(str1);

*macro to read in members is called;
*change program that reads members to a macro;

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, 05 Mar 2020 02:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629642#M186247</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-05T02:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629645#M186250</link>
      <description>&lt;P&gt;If you want to generate names like:&amp;nbsp; NSDQsh201012.zip, NSDQsh201101.zip, NSDQsh201102.zip, .... NSDQsh202002.zip. Then just look from '01DEC2010'd to '01FEB2020'd.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Much easier in DATA step code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  start= '01DEC2010'd;
  stop='01FEB2020'd;
  do offset=0 to intck('month',start,stop);
     length name $100;
     name=cats('NSDQsh',put(intnx('month',start,offset),yymmn6.),'.zip');
 ...
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could do it in macro code, but why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629645#M186250</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-04T22:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629664#M186262</link>
      <description>Tom: How would I then pass those name values (i.e. NSDQsh201012.zip, NSDQsh201101.zip, ...) iteratively into the macro parameters (i.e. .source= , and outds=)?</description>
      <pubDate>Thu, 05 Mar 2020 00:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629664#M186262</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-05T00:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629675#M186267</link>
      <description>CALL EXECUTE() the same way as in your original code.</description>
      <pubDate>Thu, 05 Mar 2020 02:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629675#M186267</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-05T02:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629717#M186291</link>
      <description>&lt;P&gt;I believe the following code does pretty much what you're asking for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** create sample data **/
%macro createSampleData(dir=%sysfunc(pathname(work)));
  %do month=1 %to 4;
    %let _month=%sysfunc(putn(&amp;amp;month,z2.));
    filename out zip "&amp;amp;dir/NSDQsh2020&amp;amp;_month..zip";
    %do day=1 %to 30;
      %let _day=%sysfunc(putn(&amp;amp;day,z2.));
      data _null_;
        file out("NSDQsh2020&amp;amp;_month.&amp;amp;_day..txt");
        put 
          "a,b,c,&amp;amp;day" /
          "x,y,z,&amp;amp;day"
        ;
        stop;
      run;
    %end;
    filename out clear;
  %end;
%mend;


/** macro definitions **/

/*
  list files in a directory. Code based on:
  https://go.documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en
*/
%macro dirlist(dir,ext,result=dir_list);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));   

  proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn;
    delete %scan(&amp;amp;result,-1,.);
    run; 
  quit;

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        /*%put &amp;amp;dir\&amp;amp;name;*/
        data _&amp;amp;result;
          length dir $200 file_name $100;
          dir="&amp;amp;dir";
          file_name="&amp;amp;name";
          output;
          stop;
        run;
        proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn; 
          append base=%scan(&amp;amp;result,-1,.) data=_%scan(&amp;amp;result,-1,.);
          run;
          delete _%scan(&amp;amp;result,-1,.);
          run;
        quit;
      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %dirlist(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend dirlist;

/* list members in zip file */
%macro zipMemList(source=, outds=zip_mem_list);
  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Read the "members" (files) from the ZIP file */
  data &amp;amp;outds(keep=zip memname);
   length zip $200 memname $200;
   zip="&amp;amp;source";
   fid=dopen("inzip");
   if fid=0 then
    stop;
   memcount=dnum(fid);
   do i=1 to memcount;
    memname=dread(fid,i);
    output;
   end;
   rc=dclose(fid);
  run;

  filename inzip clear;
%mend;


/* read member in zip file into SAS dataset */
%macro ReadMemInZip(source=, member=, outds=);
  /* Assign a fileref with the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Import a text file directly from the ZIP */
  data _tmp(compress=yes);
    infile inzip(&amp;amp;member) 
      firstobs=1 dsd dlm=',';
    input 
      (var1-var3) ($) var4;
    length source_file $ 150;
    source_file="&amp;amp;member";
  run;

  /* append to want dataset */
  proc append base=&amp;amp;outds(compress=yes) data=_tmp;
  run;quit;
  proc delete data=_tmp;
  run;quit;

  filename inzip clear;
%mend;

/* extract per zip file all the data */
%macro extract(zipfile,outds=want);
  /* extract list of members for a zip file */
  data _null_;
    length _cmd $1000;
    _cmd=cats('%',"zipMemList(source=&amp;amp;zipfile, outds=_zip_mem_list);");
    call execute(_cmd);
    stop;
  run;

  /* read all the members in zip file into SAS dataset */
  data _null_;
    set _zip_mem_list;
    length _cmd $1000;
    _cmd=cats('%ReadMemInZip(source=',zip,',member=',memname, ",outds=&amp;amp;outds);");
    call execute(_cmd);
  run;
%mend;


/** execution **/

/* define path where zip files reside */
%let source_dir=%sysfunc(pathname(work));

/* define target lib for result tables */
%let target_lib=nasdaq;
libname &amp;amp;target_lib "%sysfunc(pathname(work))";

/* define date range for zip file selection */
%let start_yyyymm=202002;
%let end_yyyymm=202003;

/* create sample zip files under this path */
%createSampleData(dir=&amp;amp;source_dir);

/* create SAS table with all zip files in folder path */
%dirlist(&amp;amp;source_dir,zip,result=_dir_list);

/* extract data. Create a table per source zip file */
%let start_dt=%sysfunc(inputn(&amp;amp;start_yyyymm,yymmn6.));
%let end_dt=%sysfunc(inputn(&amp;amp;end_yyyymm,yymmn6.));
data _null_;
  set _dir_list(
    where=(input(scan(scan(file_name,1,'.'),-1,,'kd'),yymmn6.) between &amp;amp;start_dt and &amp;amp;end_dt)
    );
  length _cmd $1000 _outds $41;
  _outds=catx('.',"&amp;amp;target_lib",scan(file_name,1,'.'));
  put _outds=;
  /* extract members list per zip file */
  _cmd=cats('%extract(zipfile=',dir,'/',file_name,',outds=',_outds,');');
  call execute(_cmd);
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 06:46:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/629717#M186291</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-05T06:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/630502#M186665</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patrick: Your reply almost has me where I need to be. I have (I think) one last question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to, as a final step, sum up observations by group.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36691iDF7F1D20E2ACEF1D/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS.PNG" alt="SAS.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using your sample data (I added an additional row of data to your code below to make my question functional), I get the above image as a final output data set. However, what I really want as a final output is a condensed version of this that sums "var4" by group "var1" and a date (one of the variables on my true data is a date. I'm just using source_file as a stand in here).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my desired output would look like this for "Nsdqsh202002".&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;source_file&lt;/TD&gt;&lt;TD&gt;sum_var4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;20200201&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;20200201&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;20200202&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;20200202&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;20200203&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;20200203&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then do the same process for the next file&amp;nbsp;"Nsdqsh202003".&lt;/P&gt;&lt;P&gt;I just can not figure out the timing of the macros and call execute commands.&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** create sample data **/
%macro createSampleData(dir=%sysfunc(pathname(work)));
  %do month=1 %to 4;
    %let _month=%sysfunc(putn(&amp;amp;month,z2.));
    filename out zip "&amp;amp;dir/NSDQsh2020&amp;amp;_month..zip";
    %do day=1 %to 30;
      %let _day=%sysfunc(putn(&amp;amp;day,z2.));
      data _null_;
        file out("NSDQsh2020&amp;amp;_month.&amp;amp;_day..txt");
        put 
          "a,b,c,&amp;amp;day" /
          "x,y,z,&amp;amp;day" /
		  "a,t,s,&amp;amp;day"
        ;
        stop;
      run;
    %end;
    filename out clear;
  %end;
%mend;


/** macro definitions **/

/*
  list files in a directory. Code based on:
  https://go.documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en
*/
%macro dirlist(dir,ext,result=dir_list);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));   

  proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn;
    delete %scan(&amp;amp;result,-1,.);
    run; 
  quit;

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        /*%put &amp;amp;dir\&amp;amp;name;*/
        data _&amp;amp;result;
          length dir $200 file_name $100;
          dir="&amp;amp;dir";
          file_name="&amp;amp;name";
          output;
          stop;
        run;
        proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn; 
          append base=%scan(&amp;amp;result,-1,.) data=_%scan(&amp;amp;result,-1,.);
          run;
          delete _%scan(&amp;amp;result,-1,.);
          run;
        quit;
      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %dirlist(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend dirlist;

/* list members in zip file */
%macro zipMemList(source=, outds=zip_mem_list);
  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Read the "members" (files) from the ZIP file */
  data &amp;amp;outds(keep=zip memname);
   length zip $200 memname $200;
   zip="&amp;amp;source";
   fid=dopen("inzip");
   if fid=0 then
    stop;
   memcount=dnum(fid);
   do i=1 to memcount;
    memname=dread(fid,i);
    output;
   end;
   rc=dclose(fid);
  run;

  filename inzip clear;
%mend;


/* read member in zip file into SAS dataset */
%macro ReadMemInZip(source=, member=, outds=);
  /* Assign a fileref with the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Import a text file directly from the ZIP */
  data _tmp(compress=yes);
    infile inzip(&amp;amp;member) 
      firstobs=1 dsd dlm=',';
    input 
      (var1-var3) ($) var4;
    length source_file $ 150;
    source_file="&amp;amp;member";
  run;

  /* append to want dataset */
  proc append base=&amp;amp;outds(compress=yes) data=_tmp;
  run;quit;
  proc delete data=_tmp;
  run;quit;

  filename inzip clear;
%mend;

/* extract per zip file all the data */
%macro extract(zipfile,outds=want);
  /* extract list of members for a zip file */
  data _null_;
    length _cmd $1000;
    _cmd=cats('%',"zipMemList(source=&amp;amp;zipfile, outds=_zip_mem_list);");
    call execute(_cmd);
    stop;
  run;

  /* read all the members in zip file into SAS dataset */
  data _null_;
    set _zip_mem_list;
    length _cmd $1000;
    _cmd=cats('%ReadMemInZip(source=',zip,',member=',memname, ",outds=&amp;amp;outds);");
    call execute(_cmd);
  run;
%mend;


/** execution **/

/* define path where zip files reside */
%let source_dir=%sysfunc(pathname(work));

/* define target lib for result tables */
%let target_lib=nasdaq;
libname &amp;amp;target_lib "%sysfunc(pathname(work))";

/* define date range for zip file selection */
%let start_yyyymm=202002;
%let end_yyyymm=202003;

/* create sample zip files under this path */
%createSampleData(dir=&amp;amp;source_dir);

/* create SAS table with all zip files in folder path */
%dirlist(&amp;amp;source_dir,zip,result=_dir_list);

/* extract data. Create a table per source zip file */
%let start_dt=%sysfunc(inputn(&amp;amp;start_yyyymm,yymmn6.));
%let end_dt=%sysfunc(inputn(&amp;amp;end_yyyymm,yymmn6.));
data _null_;
  set _dir_list(
    where=(input(scan(scan(file_name,1,'.'),-1,,'kd'),yymmn6.) between &amp;amp;start_dt and &amp;amp;end_dt)
    );
  length _cmd $1000 _outds $41;
  _outds=catx('.',"&amp;amp;target_lib",scan(file_name,1,'.'));
  put _outds=;
  /* extract members list per zip file */
  _cmd=cats('%extract(zipfile=',dir,'/',file_name,',outds=',_outds,');');
  call execute(_cmd);
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>Sun, 08 Mar 2020 17:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/630502#M186665</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-08T17:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631017#M186889</link>
      <description>&lt;P&gt;After experimenting with different techniques, I unfortunately still can't get this solved. But I'm so close thanks to all of your help. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can produce all the datasets and then subsequently summarize each one. However the size of each produced datasets makes it impossible to have all of them on my hard drive at once. Therefore it is necessary to summarize each one first before moving on to the next file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would think just adding an additional call execute (PROC SQL......) at the end of the program would do the trick. But the Proc SQL statement only runs after everything else is complete. Which defeats the purpose&lt;/P&gt;&lt;P&gt;&amp;nbsp;How can I get the timing of the commands to correspond appropriately?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 10 Mar 2020 16:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631017#M186889</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-10T16:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631019#M186890</link>
      <description>&lt;P&gt;The basic design I think you are looking for is a macro that process one file.&amp;nbsp; So it reads it, summarizes it and adds the summarized results to some master dataset of values.&amp;nbsp; So something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro run_one(filename);

* read in the file ;
data this_file;
  ....

* summarize the file;
proc summary or proc sql  
to produce this_file_summary.

* append the summary to the master file.
proc append base=maste_summary data=this_file_summary;
run;

* clear up work file;
proc delete data=this_file this_file_summary;
run;
%mend run_one;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So once you get that working then the "looping" is to just use a dataset to generate one call to the macro per file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Mar 2020 16:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631019#M186890</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-10T16:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631063#M186904</link>
      <description>&lt;P&gt;Tom: That is exactly what I want.&lt;/P&gt;&lt;P&gt;I have the middle part ready to go.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue I have is calling the files to pass through into the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have .zip files, which themselves contain .txt files. How can I get the first .txt file in the first .zip to pass through into this. Then the second .txt file in the first .zip....... then the last .txt file in the last .zip.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thinking this through, I think if I had a dataset that had a list of all the zip files and their corresponding .txt contents, then I could make something like this the first step of the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%macro run_one(source= , member= );


* Assign a fileref with the ZIP method;
filename inzip zip "&amp;amp;source";


*Import a text file directly from the ZIP;
data _tmp(compress=yes);
   infile inzip(&amp;amp;member)
     firstobs=1 dsd dlm=',';
   input
     (var1-var3) ($) var4;
run;




* read in the file ;
data this_file;
  ....

* summarize the file;
proc summary or proc sql  
to produce this_file_summary.

* append the summary to the master file.
proc append base=maste_summary data=this_file_summary;
run;

* clear up work file;
proc delete data=this_file this_file_summary;
run;
%mend run_one;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you think this could work? And if yes, do you know how I could I could create the dataset that has&amp;nbsp;a list of all my zip files and their corresponding .txt contents?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Mar 2020 20:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631063#M186904</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-10T20:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631120#M186936</link>
      <description>&lt;P&gt;Macro %ReadMemInZip() in the code I've posted earlier reads the data per zip file and then appends it to an output dataset which collects the results from all iterations.&lt;/P&gt;
&lt;P&gt;The only thing you would have to change in this macro to only collect summary data, is to aggregate the data (using Proc Summary) and then append the aggregated data.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Mar 2020 22:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631120#M186936</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-10T22:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631170#M186975</link>
      <description>&lt;P&gt;Below the whole sample code with the amended macro %ReadMemInZip() now collecting aggregated data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** create sample data **/
%macro createSampleData(dir=%sysfunc(pathname(work)));
  %do month=1 %to 4;
    %let _month=%sysfunc(putn(&amp;amp;month,z2.));
    filename out zip "&amp;amp;dir/NSDQsh2020&amp;amp;_month..zip";
    %do day=1 %to 30;
      %let _day=%sysfunc(putn(&amp;amp;day,z2.));
      data _null_;
        file out("NSDQsh2020&amp;amp;_month.&amp;amp;_day..txt");
        put 
          "a,b,c,&amp;amp;day" /
          "x,y,z,&amp;amp;day" /
		  "a,t,s,&amp;amp;day"
        ;
        stop;
      run;
    %end;
    filename out clear;
  %end;
%mend;


/** macro definitions **/

/*
  list files in a directory. Code based on:
  https://go.documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en
*/
%macro dirlist(dir,ext,result=dir_list);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));   

  proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn;
    delete %scan(&amp;amp;result,-1,.);
    run; 
  quit;

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        /*%put &amp;amp;dir\&amp;amp;name;*/
        data _&amp;amp;result;
          length dir $200 file_name $100;
          dir="&amp;amp;dir";
          file_name="&amp;amp;name";
          output;
          stop;
        run;
        proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn; 
          append base=%scan(&amp;amp;result,-1,.) data=_%scan(&amp;amp;result,-1,.);
          run;
          delete _%scan(&amp;amp;result,-1,.);
          run;
        quit;
      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %dirlist(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend dirlist;

/* list members in zip file */
%macro zipMemList(source=, outds=zip_mem_list);
  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Read the "members" (files) from the ZIP file */
  data &amp;amp;outds(keep=zip memname);
   length zip $200 memname $200;
   zip="&amp;amp;source";
   fid=dopen("inzip");
   if fid=0 then
    stop;
   memcount=dnum(fid);
   do i=1 to memcount;
    memname=dread(fid,i);
    output;
   end;
   rc=dclose(fid);
  run;

  filename inzip clear;
%mend;

/* read member in zip file into SAS dataset */
%macro ReadMemInZip(source=, member=, outds=);
  /* Assign a fileref with the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Import a text file directly from the ZIP */
  data _tmp(compress=yes);
    infile inzip(&amp;amp;member) 
      firstobs=1 dsd dlm=',';
    input 
      (var1-var3) ($) var4;
    length source_file $ 150;
    source_file="&amp;amp;member";
  run;

  proc sql;
    create table _tmp_sum as
      select 
        var1,
        source_file,
        sum(var4) as sum_var4
      from _tmp
      group by var1, source_file
      ;
  quit;

  proc delete data=_tmp;
  run;quit;

  /* append to want dataset */
  proc append base=&amp;amp;outds(compress=yes) data=_tmp_sum;
  run;quit;

  proc delete data=_tmp_sum;
  run;quit;

  filename inzip clear;
%mend;

/* extract per zip file all the data */
%macro extract(zipfile,outds=want);
  /* extract list of members for a zip file */
  data _null_;
    length _cmd $1000;
    _cmd=cats('%',"zipMemList(source=&amp;amp;zipfile, outds=_zip_mem_list);");
    call execute(_cmd);
    stop;
  run;

  /* read all the members in zip file into SAS dataset */
  data _null_;
    set _zip_mem_list;
    length _cmd $1000;
    _cmd=cats('%ReadMemInZip(source=',zip,',member=',memname, ",outds=&amp;amp;outds);");
    call execute(_cmd);
  run;
%mend;


/** execution **/

/* define path where zip files reside */
%let source_dir=%sysfunc(pathname(work));

/* define target lib for result tables */
%let target_lib=nasdaq;
libname &amp;amp;target_lib "%sysfunc(pathname(work))";

/* define date range for zip file selection */
%let start_yyyymm=202002;
%let end_yyyymm=202003;

/* create sample zip files under this path */
%createSampleData(dir=&amp;amp;source_dir);

/* create SAS table with all zip files in folder path */
%dirlist(&amp;amp;source_dir,zip,result=_dir_list);

/* extract data. Create a table per source zip file */
%let start_dt=%sysfunc(inputn(&amp;amp;start_yyyymm,yymmn6.));
%let end_dt=%sysfunc(inputn(&amp;amp;end_yyyymm,yymmn6.));
data _null_;
  set _dir_list(
    where=(input(scan(scan(file_name,1,'.'),-1,,'kd'),yymmn6.) between &amp;amp;start_dt and &amp;amp;end_dt)
    );
  length _cmd $1000 _outds $41;
  _outds=catx('.',"&amp;amp;target_lib",scan(file_name,1,'.'));
  put _outds=;
  /* extract members list per zip file */
  _cmd=cats('%extract(zipfile=',dir,'/',file_name,',outds=',_outds,');');
  call execute(_cmd);
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>Wed, 11 Mar 2020 07:55:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631170#M186975</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-11T07:55:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loop values into macro parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631334#M187048</link>
      <description>&lt;P&gt;I finally got what I need using bits and pieces from literally everyone's suggestions. And I learned a lot in the process. So thank you all very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** create sample data **/
%macro createSampleData(dir=%sysfunc(pathname(work)));
  %do month=1 %to 4;
    %let _month=%sysfunc(putn(&amp;amp;month,z2.));
    filename out zip "&amp;amp;dir/NSDQsh2020&amp;amp;_month..zip";
    %do day=1 %to 30;
      %let _day=%sysfunc(putn(&amp;amp;day,z2.));
      data _null_;
        file out("NSDQsh2020&amp;amp;_month.&amp;amp;_day..txt");
        put 
          "a,b,c,&amp;amp;day, 2020&amp;amp;_month.&amp;amp;_day" /
          "x,y,z,&amp;amp;day, 2020&amp;amp;_month.&amp;amp;_day" /
		  "a,t,s,&amp;amp;day, 2020&amp;amp;_month.&amp;amp;_day"
        ;
        stop;
      run;
    %end;
    filename out clear;
  %end;
%mend;

%macro dirlist(dir,ext,result=dir_list);
/*Gets the directory location and file name for every ZIP in the directory*/

  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));   

  proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn;
    delete %scan(&amp;amp;result,-1,.);
    run; 
  quit;

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        /*%put &amp;amp;dir\&amp;amp;name;*/
        data _&amp;amp;result;
          length dir $200 file_name $100;
          dir="&amp;amp;dir";
          file_name="&amp;amp;name";
          output;
          stop;
        run;
        proc datasets lib=%scan(work.&amp;amp;result,-2,.) nolist nowarn; 
          append base=%scan(&amp;amp;result,-1,.) data=_%scan(&amp;amp;result,-1,.);
          run;
          delete _%scan(&amp;amp;result,-1,.);
          run;
        quit;
      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %dirlist(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend dirlist;


%macro zipMemList(source=, outds=zip_mem_list);
/*Gets the file path names for all the ZIP files in the directory and the names of the .txt file contents of the ZIP files.
Produces dataset: _zip_mem_list which contains zip(the file path name) and memname (the txt contents).
*/

  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Read the "members" (files) from the ZIP file */
  data _tmp(keep=zip memname);
   length zip $200 memname $200;
   zip="&amp;amp;source";
   fid=dopen("inzip");
   if fid=0 then
    stop;
   memcount=dnum(fid);
   do i=1 to memcount;
    memname=dread(fid,i);
    output;
   end;
   rc=dclose(fid);
  run;

  filename inzip clear;

/*Append to get info for every ZIP and every .txt file*/
   proc append base=&amp;amp;outds data=_tmp;
  run;quit;
  proc delete data=_tmp;
  run;quit;
%mend;



%macro processData(source= , member=, outds= );
/*Imports data from each txt file and summarizes. Then appends to create a master file of summary data over all txt files*/

filename inzip zip "&amp;amp;source";

*Import a text file directly from the ZIP;
data _tmp(compress=yes);
   infile inzip(&amp;amp;member)
     firstobs=1 dsd dlm=',';
   input
     (var1-var3) ($) var4 var5;
run;

*Summarize the data;
proc sql;
	create table _tmp2 as
	select var1, var5, sum(var4) as sum_4
	from _tmp
	group by  var1, var5;
quit;

*Append summarized dataset and delete original;
   proc append base=&amp;amp;outds data=_tmp2;
  run;quit;
  proc delete data=_tmp2;
  run;quit;
%mend processData;



/** execution **/

/* define path where zip files reside */
%let source_dir=%sysfunc(pathname(work));

/* define target lib for result tables */
%let target_lib=nasdaq;
libname &amp;amp;target_lib "%sysfunc(pathname(work))";


/* create sample zip files under this path */
%createSampleData(dir=&amp;amp;source_dir);

/* create dataset that contains directory path names and zip file names */
%dirlist(&amp;amp;source_dir,zip,result=_dir_list);



/*create dataset that contains zip file pathnames and .txt file contents*/
data _null_;
set _dir_list;
length _name $1000;
length _cmd $1000;
_name=cats(dir,'/',file_name);
call execute('%zipMemList(source=' ||strip(_name)|| ', outds=_zip_mem_list);');
run;



/*Import each txt file and summarize the data. Append to create master dataset of summarized data across all txt files in all ZIPs*/
data _null_;
set _zip_mem_list;
call execute('%processData(source=' ||strip(zip)|| ' ,member=' ||strip(memname)|| ',outds=summarized_data);');
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>Wed, 11 Mar 2020 18:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-values-into-macro-parameters/m-p/631334#M187048</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-03-11T18:12:16Z</dc:date>
    </item>
  </channel>
</rss>

