<?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: Macro to Loop Code On All Files in a Folder, different titles, same file format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721385#M223585</link>
    <description>&lt;P&gt;You've designed this in a fashion that's almost impossible to test. I would not recommend the call execute in the way you've done here. Instead, wrap all that code (import, data, transpose, recoding etc) into a separate macro and then call the macro once from the CALL EXECUTE for each file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test each part of your code separately first as well to make sure it does what you expect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361372"&gt;@mkimmi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What I Would Like To Do:&lt;/P&gt;
&lt;P&gt;Apply the same code to all the files in a folder but the files have different file names. Can't just loop import and merge them before applying code since I need to cut some stuff out of the data set and transpose it before I merge them with one another.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've written &amp;nbsp;code that works in importing one file and doing what I like to it, but would like to write a macro that will automatically do this for all the files in a designated folder. (Each folder is a year and there are two decades worth of files to sift through). I've put the code that works on a singular file after "call execute ("&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I bolded the parts that I am having the most trouble with and am unsure if they're written correctly.&lt;/P&gt;
&lt;P&gt;What I Have:&lt;/P&gt;
&lt;DIV&gt;&lt;STRONG&gt;%macro MultImp(dir=,out=,year);&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%let rc=%str(%'dir %")&amp;amp;dir.%str(\%" %');&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;filename myfiles pipe %unquote(&amp;amp;rc);&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;data list;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;length fname $256.;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;infile myfiles truncover;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;input myfiles $100.;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;fname=quote(upcase(cats("&amp;amp;dir",'\',myfiles)));&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;out="&amp;amp;out";&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;drop myfiles;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;call execute("&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; proc import&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DBMS=XLS REPLACE;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SHEET='Sheet1';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GETNAMES=YES;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DATAROW=6;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; datafile= &lt;STRONG&gt;'||fname||'&lt;/STRONG&gt; replace ;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = pp&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; data have;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; set pp;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;proc sql noprint;&lt;/DIV&gt;
&lt;DIV&gt;select nliteral(name) into :oldname trimmed&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;from dictionary.columns&lt;/DIV&gt;
&lt;DIV&gt;where libname='WORK' and memname='HAVE' and varnum=1&lt;/DIV&gt;
&lt;DIV&gt;;&lt;/DIV&gt;
&lt;DIV&gt;quit;&lt;/DIV&gt;
&lt;DIV&gt;data want;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;set have (rename=(&amp;amp;oldname=STATE));&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data uu (Keep=State B &amp;nbsp;R);&lt;/DIV&gt;
&lt;DIV&gt;set want;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if not State&amp;gt;=0 and B='' then B='Total';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if State='' and R='' or R&amp;lt;0 then delete;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if B='$1 under $25,000' then B='Lower';&lt;/DIV&gt;
&lt;DIV&gt;if B='$25,000 under $50,000' then B='Lower Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$50,000 under $75,000' then B='Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$75,000 under $100,000' then B='Upper Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$100,000 under $200,000' then B='Upper';&lt;/DIV&gt;
&lt;DIV&gt;if B='$200,000 or more' then B='Rich';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc sort data=uu;&lt;/DIV&gt;
&lt;DIV&gt;by State B;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc transpose data=uu out=uu_transposed;&lt;/DIV&gt;
&lt;DIV&gt;by State;&lt;/DIV&gt;
&lt;DIV&gt;id B;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data mm (Keep=State C1 C2 C3 Class Year);&lt;/DIV&gt;
&lt;DIV&gt;set uu_transposed;&lt;/DIV&gt;
&lt;DIV&gt;C1=Lower;&lt;/DIV&gt;
&lt;DIV&gt;C2=Lower_Middle + Middle + Upper_Middle;&lt;/DIV&gt;
&lt;DIV&gt;C3=Upper + Rich;&lt;/DIV&gt;
&lt;DIV&gt;array values C1-C3;&lt;/DIV&gt;
&lt;DIV&gt;largest = max(of values[*]);&lt;/DIV&gt;
&lt;DIV&gt;index &amp;nbsp; &amp;nbsp;= whichn(largest, of values[*]);&lt;/DIV&gt;
&lt;DIV&gt;LENGTH Class $ 10;&lt;/DIV&gt;
&lt;DIV&gt;Class = vname(values[index]);&lt;/DIV&gt;
&lt;DIV&gt;if Class = 'C1' then Class='Lower';&lt;/DIV&gt;
&lt;DIV&gt;if Class ='C2' then Class ='Middle';&lt;/DIV&gt;
&lt;DIV&gt;if Class ='C3' then Class ='Upper';&lt;/DIV&gt;
&lt;DIV&gt;Year=&lt;STRONG&gt;&amp;amp;year.&lt;/STRONG&gt;;&lt;/DIV&gt;
&lt;DIV&gt;Drop index largest;&lt;/DIV&gt;
&lt;DIV&gt;Rename _Name_=Name;&lt;/DIV&gt;
&lt;DIV&gt;Name='Total Number of Returns';&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;proc append base=&lt;STRONG&gt;||out||&lt;/STRONG&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data=mm;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;");&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;filename myfiles clear;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%mend;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%MultImp(dir='/folders/myfolders/sasuser.v94/zipcode2014/',merged2014, 2014)&lt;/STRONG&gt;;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Please let me know if this doesn't make sense, or if there is any advice for this! Thanks!&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;</description>
    <pubDate>Tue, 23 Feb 2021 20:17:47 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-02-23T20:17:47Z</dc:date>
    <item>
      <title>Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721377#M223580</link>
      <description>&lt;P&gt;What I Would Like To Do:&lt;/P&gt;&lt;P&gt;Apply the same code to all the files in a folder but the files have different file names. Can't just loop import and merge them before applying code since I need to cut some stuff out of the data set and transpose it before I merge them with one another.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've written &amp;nbsp;code that works in importing one file and doing what I like to it, but would like to write a macro that will automatically do this for all the files in a designated folder. (Each folder is a year and there are two decades worth of files to sift through). I've put the code that works on a singular file after "call execute ("&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I bolded the parts that I am having the most trouble with and am unsure if they're written correctly.&lt;/P&gt;&lt;P&gt;What I Have:&lt;/P&gt;&lt;DIV&gt;&lt;STRONG&gt;%macro MultImp(dir=,out=,year);&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;%let rc=%str(%'dir %")&amp;amp;dir.%str(\%" %');&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;filename myfiles pipe %unquote(&amp;amp;rc);&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;data list;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;length fname $256.;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;infile myfiles truncover;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;input myfiles $100.;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;fname=quote(upcase(cats("&amp;amp;dir",'\',myfiles)));&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;out="&amp;amp;out";&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;drop myfiles;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;call execute("&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; proc import&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DBMS=XLS REPLACE;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SHEET='Sheet1';&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GETNAMES=YES;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DATAROW=6;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; datafile= &lt;STRONG&gt;'||fname||'&lt;/STRONG&gt; replace ;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = pp&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; data have;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; set pp;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;proc sql noprint;&lt;/DIV&gt;&lt;DIV&gt;select nliteral(name) into :oldname trimmed&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;from dictionary.columns&lt;/DIV&gt;&lt;DIV&gt;where libname='WORK' and memname='HAVE' and varnum=1&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;quit;&lt;/DIV&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;set have (rename=(&amp;amp;oldname=STATE));&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data uu (Keep=State B &amp;nbsp;R);&lt;/DIV&gt;&lt;DIV&gt;set want;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if not State&amp;gt;=0 and B='' then B='Total';&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if State='' and R='' or R&amp;lt;0 then delete;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if B='$1 under $25,000' then B='Lower';&lt;/DIV&gt;&lt;DIV&gt;if B='$25,000 under $50,000' then B='Lower Middle';&lt;/DIV&gt;&lt;DIV&gt;if B='$50,000 under $75,000' then B='Middle';&lt;/DIV&gt;&lt;DIV&gt;if B='$75,000 under $100,000' then B='Upper Middle';&lt;/DIV&gt;&lt;DIV&gt;if B='$100,000 under $200,000' then B='Upper';&lt;/DIV&gt;&lt;DIV&gt;if B='$200,000 or more' then B='Rich';&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc sort data=uu;&lt;/DIV&gt;&lt;DIV&gt;by State B;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc transpose data=uu out=uu_transposed;&lt;/DIV&gt;&lt;DIV&gt;by State;&lt;/DIV&gt;&lt;DIV&gt;id B;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data mm (Keep=State C1 C2 C3 Class Year);&lt;/DIV&gt;&lt;DIV&gt;set uu_transposed;&lt;/DIV&gt;&lt;DIV&gt;C1=Lower;&lt;/DIV&gt;&lt;DIV&gt;C2=Lower_Middle + Middle + Upper_Middle;&lt;/DIV&gt;&lt;DIV&gt;C3=Upper + Rich;&lt;/DIV&gt;&lt;DIV&gt;array values C1-C3;&lt;/DIV&gt;&lt;DIV&gt;largest = max(of values[*]);&lt;/DIV&gt;&lt;DIV&gt;index &amp;nbsp; &amp;nbsp;= whichn(largest, of values[*]);&lt;/DIV&gt;&lt;DIV&gt;LENGTH Class $ 10;&lt;/DIV&gt;&lt;DIV&gt;Class = vname(values[index]);&lt;/DIV&gt;&lt;DIV&gt;if Class = 'C1' then Class='Lower';&lt;/DIV&gt;&lt;DIV&gt;if Class ='C2' then Class ='Middle';&lt;/DIV&gt;&lt;DIV&gt;if Class ='C3' then Class ='Upper';&lt;/DIV&gt;&lt;DIV&gt;Year=&lt;STRONG&gt;&amp;amp;year.&lt;/STRONG&gt;;&lt;/DIV&gt;&lt;DIV&gt;Drop index largest;&lt;/DIV&gt;&lt;DIV&gt;Rename _Name_=Name;&lt;/DIV&gt;&lt;DIV&gt;Name='Total Number of Returns';&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;proc append base=&lt;STRONG&gt;||out||&lt;/STRONG&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data=mm;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;");&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;filename myfiles clear;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;%mend;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;%MultImp(dir='/folders/myfolders/sasuser.v94/zipcode2014/',merged2014, 2014)&lt;/STRONG&gt;;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Please let me know if this doesn't make sense, or if there is any advice for this! Thanks!&lt;/DIV&gt;</description>
      <pubDate>Tue, 23 Feb 2021 19:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721377#M223580</guid>
      <dc:creator>mkimmi</dc:creator>
      <dc:date>2021-02-23T19:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721385#M223585</link>
      <description>&lt;P&gt;You've designed this in a fashion that's almost impossible to test. I would not recommend the call execute in the way you've done here. Instead, wrap all that code (import, data, transpose, recoding etc) into a separate macro and then call the macro once from the CALL EXECUTE for each file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test each part of your code separately first as well to make sure it does what you expect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361372"&gt;@mkimmi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What I Would Like To Do:&lt;/P&gt;
&lt;P&gt;Apply the same code to all the files in a folder but the files have different file names. Can't just loop import and merge them before applying code since I need to cut some stuff out of the data set and transpose it before I merge them with one another.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've written &amp;nbsp;code that works in importing one file and doing what I like to it, but would like to write a macro that will automatically do this for all the files in a designated folder. (Each folder is a year and there are two decades worth of files to sift through). I've put the code that works on a singular file after "call execute ("&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I bolded the parts that I am having the most trouble with and am unsure if they're written correctly.&lt;/P&gt;
&lt;P&gt;What I Have:&lt;/P&gt;
&lt;DIV&gt;&lt;STRONG&gt;%macro MultImp(dir=,out=,year);&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%let rc=%str(%'dir %")&amp;amp;dir.%str(\%" %');&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;filename myfiles pipe %unquote(&amp;amp;rc);&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;data list;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;length fname $256.;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;infile myfiles truncover;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;input myfiles $100.;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;fname=quote(upcase(cats("&amp;amp;dir",'\',myfiles)));&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;out="&amp;amp;out";&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;drop myfiles;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;call execute("&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; proc import&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DBMS=XLS REPLACE;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SHEET='Sheet1';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GETNAMES=YES;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DATAROW=6;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; datafile= &lt;STRONG&gt;'||fname||'&lt;/STRONG&gt; replace ;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = pp&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; data have;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; set pp;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;proc sql noprint;&lt;/DIV&gt;
&lt;DIV&gt;select nliteral(name) into :oldname trimmed&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;from dictionary.columns&lt;/DIV&gt;
&lt;DIV&gt;where libname='WORK' and memname='HAVE' and varnum=1&lt;/DIV&gt;
&lt;DIV&gt;;&lt;/DIV&gt;
&lt;DIV&gt;quit;&lt;/DIV&gt;
&lt;DIV&gt;data want;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;set have (rename=(&amp;amp;oldname=STATE));&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data uu (Keep=State B &amp;nbsp;R);&lt;/DIV&gt;
&lt;DIV&gt;set want;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if not State&amp;gt;=0 and B='' then B='Total';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if State='' and R='' or R&amp;lt;0 then delete;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;if B='$1 under $25,000' then B='Lower';&lt;/DIV&gt;
&lt;DIV&gt;if B='$25,000 under $50,000' then B='Lower Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$50,000 under $75,000' then B='Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$75,000 under $100,000' then B='Upper Middle';&lt;/DIV&gt;
&lt;DIV&gt;if B='$100,000 under $200,000' then B='Upper';&lt;/DIV&gt;
&lt;DIV&gt;if B='$200,000 or more' then B='Rich';&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc sort data=uu;&lt;/DIV&gt;
&lt;DIV&gt;by State B;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc transpose data=uu out=uu_transposed;&lt;/DIV&gt;
&lt;DIV&gt;by State;&lt;/DIV&gt;
&lt;DIV&gt;id B;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data mm (Keep=State C1 C2 C3 Class Year);&lt;/DIV&gt;
&lt;DIV&gt;set uu_transposed;&lt;/DIV&gt;
&lt;DIV&gt;C1=Lower;&lt;/DIV&gt;
&lt;DIV&gt;C2=Lower_Middle + Middle + Upper_Middle;&lt;/DIV&gt;
&lt;DIV&gt;C3=Upper + Rich;&lt;/DIV&gt;
&lt;DIV&gt;array values C1-C3;&lt;/DIV&gt;
&lt;DIV&gt;largest = max(of values[*]);&lt;/DIV&gt;
&lt;DIV&gt;index &amp;nbsp; &amp;nbsp;= whichn(largest, of values[*]);&lt;/DIV&gt;
&lt;DIV&gt;LENGTH Class $ 10;&lt;/DIV&gt;
&lt;DIV&gt;Class = vname(values[index]);&lt;/DIV&gt;
&lt;DIV&gt;if Class = 'C1' then Class='Lower';&lt;/DIV&gt;
&lt;DIV&gt;if Class ='C2' then Class ='Middle';&lt;/DIV&gt;
&lt;DIV&gt;if Class ='C3' then Class ='Upper';&lt;/DIV&gt;
&lt;DIV&gt;Year=&lt;STRONG&gt;&amp;amp;year.&lt;/STRONG&gt;;&lt;/DIV&gt;
&lt;DIV&gt;Drop index largest;&lt;/DIV&gt;
&lt;DIV&gt;Rename _Name_=Name;&lt;/DIV&gt;
&lt;DIV&gt;Name='Total Number of Returns';&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;proc append base=&lt;STRONG&gt;||out||&lt;/STRONG&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data=mm;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;");&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;filename myfiles clear;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%mend;&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;%MultImp(dir='/folders/myfolders/sasuser.v94/zipcode2014/',merged2014, 2014)&lt;/STRONG&gt;;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Please let me know if this doesn't make sense, or if there is any advice for this! Thanks!&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Tue, 23 Feb 2021 20:17:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721385#M223585</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-23T20:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721421#M223595</link>
      <description>&lt;P&gt;Understood! I will definitely do that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is primarily concerned with the bolded part: all the stuff before call execute. I'm unsure how I could design the macro to open a folder (which is what I hoped to set "dir=" to), and then apply said code (after call execute--doesn't matter what the code is) to all the files in that folder regardless of their names?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 23:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721421#M223595</guid>
      <dc:creator>mkimmi</dc:creator>
      <dc:date>2021-02-23T23:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721424#M223596</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; -- I saw you link &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type&lt;/A&gt; in another thread--gonna try it out! looks like something that I'm looking for</description>
      <pubDate>Tue, 23 Feb 2021 23:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721424#M223596</guid>
      <dc:creator>mkimmi</dc:creator>
      <dc:date>2021-02-23T23:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721428#M223599</link>
      <description>&lt;P&gt;Is the question how to find the names of all of the files in a directory?&lt;/P&gt;
&lt;P&gt;It looks like you are trying to run an operating system command to do this.&amp;nbsp; In that case you need to know what operating system your SAS session is running under and what command you can use to list the files.&lt;/P&gt;
&lt;P&gt;If you SAS session is running under Windows then the DIR command with the /B option is the simplest.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dir=x:\some directory\;
data list;
  infile %sysfunc(quote(dir /b "&amp;amp;dir")) pipe truncover;
  input fname $256. ;
  fname=cats("&amp;amp;dir",fname);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note it is easier if you just add the \ to the end of the macro variable with the directory name.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 00:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721428#M223599</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-24T00:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721447#M223611</link>
      <description>I was trying to import all files in a directory, and for each file, apply a code that'll change into a new dataset&lt;BR /&gt;&lt;BR /&gt;So far, based on the code here: &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type&lt;/A&gt;, I understand how make a list of all the file names, then make a separate macro to proc import each of them. Then at the end, write a function to call the separate macro for each entry on the list.&lt;BR /&gt;&lt;BR /&gt;What I'm trying to do now is to add to that separate macro my code, but I'm a bit stumped with the "catt('test', put(_n_, z2.)), ');');" part of the last step copied below:&lt;BR /&gt;%*Call macro once for each entry in the list table created from the %list_files() macro;&lt;BR /&gt;data _null_;&lt;BR /&gt;set list;&lt;BR /&gt;string = catt('%import_file(', dir, ', ', name,', ', catt('test', put(_n_, z2.)), ');');&lt;BR /&gt;call execute (string);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 24 Feb 2021 01:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721447#M223611</guid>
      <dc:creator>mkimmi</dc:creator>
      <dc:date>2021-02-24T01:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721449#M223613</link>
      <description>&lt;P&gt;Add a PUT statement to write STRING to the SAS log to see if it generates correct macro calls:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%*Call macro once for each entry in the list table created from the %list_files() macro;
data _null_;
set list;
string = catt('%import_file(', dir, ', ', name,', ', catt('test', put(_n_, z2.)), ');');
put string = ;
* call execute (string);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Feb 2021 02:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721449#M223613</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-02-24T02:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721608#M223689</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;I recommend using this approach : Tutorial on converting a working program to a macro&lt;/STRONG&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"&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;Examples of common macro usage - includes an example of getting a file listing&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another example you may find helpful:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;How to add data to a regular report and then report on it daily&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c" target="_blank"&gt;https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 16:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721608#M223689</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-24T16:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Loop Code On All Files in a Folder, different titles, same file format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721914#M223800</link>
      <description>Hi yes thank you--I actually ended up creating my code based on this GitHub file you posted on another thread! so thank you!&lt;BR /&gt;&lt;BR /&gt;Just more minor problems come as I continue forth</description>
      <pubDate>Thu, 25 Feb 2021 16:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Loop-Code-On-All-Files-in-a-Folder-different-titles/m-p/721914#M223800</guid>
      <dc:creator>mkimmi</dc:creator>
      <dc:date>2021-02-25T16:04:04Z</dc:date>
    </item>
  </channel>
</rss>

