<?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 Loops for Read In in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729425#M80341</link>
    <description>&lt;P&gt;Apologies for double-posting; however, I don't think I was clear enough in my original post--there is more nuance to my problem, I think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first issue, is that I am trying establish libnames for a series of folders. I would like to do this in a loop and using a macro. The macro is year = 2020, but some of the year indications in the filepaths are the full four-digit year YYYY and others are the two-digit year YY. This is an established program and it is best to only use the existing macros. (I do realize it would be easiest to create a macro equal to the two-digit year.) What I have is what is below, but I'd like to condense it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let lastyear = 2020;        

libname  data11"S:\Projects\Data\2011\Intermediate";
libname  data12"S:\Projects\Data\2012\Intermediate";
libname  data13"S:\Projects\Data\2013\Intermediate";
libname  data14"S:\Projects\Data\2014\Intermediate";
libname  data15"S:\Projects\Data\2015\Intermediate";
libname  data16"S:\Projects\Data\2016\Intermediate";
libname  data17"S:\Projects\Data\2017\Intermediate";
libname  data18"S:\Projects\Data\2018\Intermediate";
libname  data19"S:\Projects\Data\2019\Intermediate";
libname  data20"S:\Projects\Data\2020\Intermediate";
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My second goal is to improve the efficiency of reading in a set of data sets in a set statement. Again, these have a consistent naming convention and again I am working with a macro that is four digit year of the most recent year of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	%let lastyear = 2020;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;        data new_data;
	  	  set   data18.file_name(in=_18)
			data19.file_name(in=_19)
			data20.file_name(in=_20);

	  new_var=old_var(mn, dy, yr);
	run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Mar 2021 15:42:30 GMT</pubDate>
    <dc:creator>raivester</dc:creator>
    <dc:date>2021-03-26T15:42:30Z</dc:date>
    <item>
      <title>Loops for Read In</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729425#M80341</link>
      <description>&lt;P&gt;Apologies for double-posting; however, I don't think I was clear enough in my original post--there is more nuance to my problem, I think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first issue, is that I am trying establish libnames for a series of folders. I would like to do this in a loop and using a macro. The macro is year = 2020, but some of the year indications in the filepaths are the full four-digit year YYYY and others are the two-digit year YY. This is an established program and it is best to only use the existing macros. (I do realize it would be easiest to create a macro equal to the two-digit year.) What I have is what is below, but I'd like to condense it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let lastyear = 2020;        

libname  data11"S:\Projects\Data\2011\Intermediate";
libname  data12"S:\Projects\Data\2012\Intermediate";
libname  data13"S:\Projects\Data\2013\Intermediate";
libname  data14"S:\Projects\Data\2014\Intermediate";
libname  data15"S:\Projects\Data\2015\Intermediate";
libname  data16"S:\Projects\Data\2016\Intermediate";
libname  data17"S:\Projects\Data\2017\Intermediate";
libname  data18"S:\Projects\Data\2018\Intermediate";
libname  data19"S:\Projects\Data\2019\Intermediate";
libname  data20"S:\Projects\Data\2020\Intermediate";
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My second goal is to improve the efficiency of reading in a set of data sets in a set statement. Again, these have a consistent naming convention and again I am working with a macro that is four digit year of the most recent year of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	%let lastyear = 2020;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;        data new_data;
	  	  set   data18.file_name(in=_18)
			data19.file_name(in=_19)
			data20.file_name(in=_20);

	  new_var=old_var(mn, dy, yr);
	run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 15:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729425#M80341</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-03-26T15:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: Loops for Read In</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729438#M80343</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/326526"&gt;@raivester&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Apologies for double-posting; however, I don't think I was clear enough in my original post--there is more nuance to my problem, I think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first issue, is that I am trying establish libnames for a series of folders. I would like to do this in a loop and using a macro. The macro is year = 2020, but some of the year indications in the filepaths are the full four-digit year YYYY and others are the two-digit year YY. This is an established program and it is best to only use the existing macros. (I do realize it would be easiest to create a macro equal to the two-digit year.) What I have is what is below, but I'd like to condense it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let lastyear = 2020;        

libname  data11"S:\Projects\Data\2011\Intermediate";
libname  data12"S:\Projects\Data\2012\Intermediate";
libname  data13"S:\Projects\Data\2013\Intermediate";
libname  data14"S:\Projects\Data\2014\Intermediate";
libname  data15"S:\Projects\Data\2015\Intermediate";
libname  data16"S:\Projects\Data\2016\Intermediate";
libname  data17"S:\Projects\Data\2017\Intermediate";
libname  data18"S:\Projects\Data\2018\Intermediate";
libname  data19"S:\Projects\Data\2019\Intermediate";
libname  data20"S:\Projects\Data\2020\Intermediate";
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My second goal is to improve the efficiency of reading in a set of data sets in a set statement. Again, these have a consistent naming convention and again I am working with a macro that is four digit year of the most recent year of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	%let lastyear = 2020;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;        data new_data;
	  	  set   data18.file_name(in=_18)
			data19.file_name(in=_19)
			data20.file_name(in=_20);

	  new_var=old_var(mn, dy, yr);
	run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is a data step function that will create libraries imaginatively named LIBNAME.&lt;/P&gt;
&lt;P&gt;Basically called such as&lt;/P&gt;
&lt;PRE&gt;data _null_;
   rc= libname('mylib','path');
run;&lt;/PRE&gt;
&lt;P&gt;RC is a return code you could test to see if the assignment is successful. If the second parameter is blank or missing then SAS will clear the libname assignmen.&lt;/P&gt;
&lt;P&gt;So if you can create a character variable with the libref and path you want you place the variables in the function:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   length libref $ 8;
   do yr=2011 to 2020;
      libref=cats('data',yr);
      path= cats('S:\Projects\Data\',yr,'\Intermediate');
      rc = libname(libref,path);
   end;
run;&lt;/PRE&gt;
&lt;P&gt;You really do not save anything by using a 2-digit year in your libref but you can play with the above if you really must.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example for set statement does not provide any rule for when the list would start. In the latest versions of SAS something like this using a macro %do loop should work:&lt;/P&gt;
&lt;PRE&gt;data new_data;
   set   
    %do year=2018 %to &amp;amp;lastyear.;
        data&amp;amp;year..file_name(in=_&amp;amp;year.)
    %end;
    ;  /*end the SET statement*/

    new_var=old_var(mn, dy, yr);
run;&lt;/PRE&gt;
&lt;P&gt;Note that the macro processor uses a . to indicate the end of the variable so when you need a . for the libname.dsname you need to provide an extra or it will get eaten.&lt;/P&gt;
&lt;P&gt;Again I see no advantage to removing 2 characters in the libname or the IN variables, especially since you are providing "lastyear" in 4 digit form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 16:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729438#M80343</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-26T16:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loops for Read In</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729445#M80344</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the SET option INDSNAME:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%let lastyear = 2020; 

        data new_data;
	  	  set   data18.file_name
			    data19.file_name
			    data20.file_name 
          indsname=_dsname;
          dsname=_dsname;*optional. Do this or else it will not be added to the output;
	  
	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>Fri, 26 Mar 2021 17:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loops-for-Read-In/m-p/729445#M80344</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-03-26T17:01:47Z</dc:date>
    </item>
  </channel>
</rss>

