<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to apply derived value from file names to corresponding files in a directory in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541276#M149417</link>
    <description>&lt;P&gt;The data step can know the name of the dataset that is contributing the current observation. Use the INDSNAME= option on the SET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example here is a way to combine all of the "monthly" datasets into a single dataset and add the month as a date variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname files 'C:\Files';

proc contents noprint data=files._all_ out=contents; run;

proc sql noprint;
  select distinct catx('.',libname,memname)
    into :dslist separated by ' '
    from contents
  ;
quit;

data want ;
   length filedate 8 dsname $50 ;
   set &amp;amp;dslist indsname=dsname ;
   filedate = input(cats('01',scan(dsname,-1,'_')),date9.);
   format filedate date9.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Mar 2019 01:28:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-03-08T01:28:22Z</dc:date>
    <item>
      <title>how to apply derived value from file names to corresponding files in a directory</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541211#M149393</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;looking for some help as how to apply the derived variable values from file names. I feel like there is an easy macro to do this and my mind is just not going there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code generates two variables - one with the path/filename the other the corresponding file date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to insert the file date value with the dataset of the corresponding file name as a variable to be associated with each observation in the particular file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;filename examples&lt;/P&gt;&lt;P&gt;xxxxxxxxxxxxxxxx_dec2018&lt;/P&gt;&lt;P&gt;xxxxxxxxxxxxxxxx_jan2019&lt;/P&gt;&lt;P&gt;xxxxxxxxxxxxxxxx_feb2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;filename&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; fname &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;pipe&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;'dir C:\files\*.sas7bdat /b/s'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;infile&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; fname;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; filename :&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;$100.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;new_var=substr(filename,prxmatch(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'/_\d{8}\./'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;, filename)+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;31&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;7&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;date1=input(new_var,&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;anydtdte7.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;lastDay=intnx (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'month'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,date1,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'E'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;filedate=lastday;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;format&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; filedate &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;keep&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; filename filedate;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 20:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541211#M149393</guid>
      <dc:creator>William_B</dc:creator>
      <dc:date>2019-03-07T20:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: how to apply derived value from file names to corresponding files in a directory</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541275#M149416</link>
      <description>&lt;P&gt;Why are you looking for 8 digits when the filename doesn't have them?&lt;/P&gt;
&lt;P&gt;This works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST;
  input FILENAME :&amp;amp;$100.;
  DATEC   = substr(FILENAME,prxmatch('/_\w{3}\d{4}\./', FILENAME)+1);
  DATEN   = input(DATEC,anydtdte7.);
  FILEDATE= intnx ('month',DATEN,0,'E');
  put FILEDATE= date9.;
cards;
xxxxxxxxxxxxxxxx_JAN2019.
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;FILEDATE=31JAN2019&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Mar 2019 01:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541275#M149416</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-03-08T01:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to apply derived value from file names to corresponding files in a directory</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541276#M149417</link>
      <description>&lt;P&gt;The data step can know the name of the dataset that is contributing the current observation. Use the INDSNAME= option on the SET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example here is a way to combine all of the "monthly" datasets into a single dataset and add the month as a date variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname files 'C:\Files';

proc contents noprint data=files._all_ out=contents; run;

proc sql noprint;
  select distinct catx('.',libname,memname)
    into :dslist separated by ' '
    from contents
  ;
quit;

data want ;
   length filedate 8 dsname $50 ;
   set &amp;amp;dslist indsname=dsname ;
   filedate = input(cats('01',scan(dsname,-1,'_')),date9.);
   format filedate date9.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Mar 2019 01:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-apply-derived-value-from-file-names-to-corresponding/m-p/541276#M149417</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-08T01:28:22Z</dc:date>
    </item>
  </channel>
</rss>

