<?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: Looping through Files to Import SAS Data Sets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672781#M202234</link>
    <description>&lt;P&gt;Do it in a single DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_all;

%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  libname i&amp;amp;cm. "\\SFLC-SAS-AP\pod-data3\MDR\&amp;amp;cm.\ValueOfInventory\";
%end;

data work.oh_di_all;
set
%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  i&amp;amp;cm..oh_di_all_:
%end;
;
run;

%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  libname i&amp;amp;cm. clear;
%end;

%mend;

%do_all&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Jul 2020 11:56:23 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-07-28T11:56:23Z</dc:date>
    <item>
      <title>Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672772#M202231</link>
      <description>&lt;P&gt;I am hoping to get code that will loop through the past 49 months of data and import the SAS dataset saved there and then append all 49 together with a new column for the date from the file name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA _NULL_;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;DO i&amp;nbsp; = 1 to 49;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATA _NULL_;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MP1DB = INTNX('month',today(),-i,'B');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CALL SYMPUTX ('CURRENTMONTH',CATS(SUBSTR(PU&lt;/SPAN&gt;&lt;SPAN&gt;T(MP1DB,yymmn6.),1,4), '-',SUBSTR(PUT(MP1DB,yymmn6.),&lt;/SPAN&gt;&lt;SPAN&gt;5,2)));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RUN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; libname wm&amp;nbsp; "\\SFLC-SAS-AP\pod-data3\MDR\&amp;amp;&lt;/SPAN&gt;&lt;SPAN&gt;CURRENTMONTH.\ValueOfInventory&lt;/SPAN&gt;&lt;SPAN&gt;\";&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; data work.oh_di_all_MPi;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;set wm.oh_di_all_:;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;month=&amp;amp;CURRENTMONTH.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; run;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;END;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RUN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;%macro append;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;data work.oh_di_all;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%do&amp;nbsp; i =&amp;nbsp; 1 %to 49;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; oh_di_all_MP&amp;amp;i&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%mend append;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;%append;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 11:32:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672772#M202231</guid>
      <dc:creator>ErinKSimmons</dc:creator>
      <dc:date>2020-07-28T11:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672777#M202233</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe something like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
%macro loop(end);

%DO i = 1 %to &amp;amp;end.;
%let CURRENTMONTH = %sysfunc(INTNX(month, %sysfunc(today()), -&amp;amp;i, B), yymmd7.);

libname wm  "\\SFLC-SAS-AP\pod-data3\MDR\&amp;amp;CURRENTMONTH.\ValueOfInventory\";

data oh_di_all_MP&amp;amp;i;
  set wm.oh_di_all_:;
  where month = "&amp;amp;CURRENTMONTH.";
run;
%END;
%mend loop;
%loop(49)

data work.oh_di_all;
  set oh_di_all_MP1 -- oh_di_all_MP49
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume that month is a character variable since you wrote ```month = &amp;amp;CURRENTMONTH.``` and CURRENTMONTH contains a hyphen(-).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 11:48:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672777#M202233</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-07-28T11:48:13Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672781#M202234</link>
      <description>&lt;P&gt;Do it in a single DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_all;

%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  libname i&amp;amp;cm. "\\SFLC-SAS-AP\pod-data3\MDR\&amp;amp;cm.\ValueOfInventory\";
%end;

data work.oh_di_all;
set
%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  i&amp;amp;cm..oh_di_all_:
%end;
;
run;

%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  libname i&amp;amp;cm. clear;
%end;

%mend;

%do_all&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jul 2020 11:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672781#M202234</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-28T11:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672789#M202240</link>
      <description>&lt;P&gt;You don't "import" a SAS dataset. You just access it.&lt;/P&gt;
&lt;P&gt;What is the pattern in the names of the files?&amp;nbsp; &amp;nbsp;Looks like the one level of the directory is in the month as YYYY-MM?&lt;/P&gt;
&lt;P&gt;Do you really want to combine multiple datasets from each of those directories?&amp;nbsp; Or is there just one dataset and you are using the : because the end of the dataset name varies?&amp;nbsp; If the later how does it vary?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also looks like you are trying to add a variable that indicates which directory the data came from.&amp;nbsp; Is that really needed?&amp;nbsp; Don't the dataset already have a variable with this information?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 12:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672789#M202240</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-28T12:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672792#M202241</link>
      <description>Each month we pull a snapshot of the Value of Inventory. We try to run this the first of the month, but if that falls on a weekend, we do it the following Monday. This is only one of several monthly files we output. &lt;BR /&gt;&lt;BR /&gt;The files all follow the name pattern OH_DI_ALL_DDMONYYY where DDMONYYYY is the date the data was run since it won't necessarily be the first of the month. &lt;BR /&gt;&lt;BR /&gt;The only thing that changes in the directory name is the month portion. There is a subfolder ValueofInventory in each month's subfolder. &lt;BR /&gt;&lt;BR /&gt;I want to access one SAS dataset from each month within the ValueofInventory subfolder in that month folder. I am using the : because I want to go into 49 month folders into the ValueofInventory subfolder and access the one SAS dataset, whose subscript date (DDMONYYYY) may vary. &lt;BR /&gt;&lt;BR /&gt;Yes I want to add the variable to indicate what directory it comes from because it was a snapshot in time and never originally intended to be concatenated but rather use just one file at a time. Now they want to append to track a historical value from all of these different snapshots so I have to create this variable to do the time series graphing.</description>
      <pubDate>Tue, 28 Jul 2020 12:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672792#M202241</guid>
      <dc:creator>ErinKSimmons</dc:creator>
      <dc:date>2020-07-28T12:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672797#M202245</link>
      <description>&lt;P&gt;Expand the data step from my previous code to get the month:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.oh_di_all;
set
%do i = -49 %to -1;
  %let cm = %sysfunc(intnx(month,%sysfunc(today()),&amp;amp;i),yymmd7.);
  i&amp;amp;cm..oh_di_all_:
%end;
  indsname=ds
;
month = substr(scan(ds,1,'.'),2);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jul 2020 12:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672797#M202245</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-28T12:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through Files to Import SAS Data Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672808#M202253</link>
      <description>&lt;P&gt;I would just avoid macro for this as working with so many files is probably easier with data steps.&lt;/P&gt;
&lt;P&gt;So first generate a libref for each of your source directories.&amp;nbsp; Let's name the librefs as ZZYYYYMM.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data librefs ;
  length libref $8 rc date 8 month $7 path $256 ;
  today=today();
  format today date date9.;
  do offset=-49 to -1 
    date = intnx('month',today,offset,'b');
    month = put(date,yymmd7.);
    libref=cats('ZZ',put(date,yymmn6.));
    path=catx('\','\\SFLC-SAS-AP\pod-data3\MDR',month,'ValueOfInventory');
    rc=libname(libref,path);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then find the actual datasets you want to read by querying the metadata.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table datasets as 
    select b.libref,b.memname,a.date,a.month,a.path
    from librefs a
    inner join dictionary.members b
    on a.libref = b.libref 
    and b.memname like 'OH^_DI^_ALL^_%' escape '^'
    order by b.libref,b.memname 
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use a data step to generate a SET statement that lists each actual dataset found.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set datasets end=eof;
  file code lrecl=70 ;
  if _n_=1 then put 'set ' @;
  put  libname +(-1) '.' memname @;
  if eof then put ' indsname=indsname ;' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then use that SET statement in a data step.&amp;nbsp; You can pull out the MONTH and actual DATE from the LIBNAME and MEMNAME part of the dataset being read.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data oh_di_all ;
  length indsname $41 month $7 date 8 ;
%include temp / source2;
  month=substr(scan(indsname,1,'.'),3);
  date = input(substr(indsname,length(indsname)-8),date9.);
  format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jul 2020 13:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-Files-to-Import-SAS-Data-Sets/m-p/672808#M202253</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-28T13:01:44Z</dc:date>
    </item>
  </channel>
</rss>

