<?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 extract variable from filename? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559131#M17110</link>
    <description>All the new datasets are labeled _1 to _3000, I think from the out option in proc import. I don't fully understand how it works (found the code on another sas communities post) but it does import all files into the work directory. now, just need the date.</description>
    <pubDate>Wed, 15 May 2019 19:39:54 GMT</pubDate>
    <dc:creator>pamplemousse822</dc:creator>
    <dc:date>2019-05-15T19:39:54Z</dc:date>
    <item>
      <title>How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559100#M17104</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a folder with ~3000 .xls files. I am trying to (1) import all into sas, and (2) extract each file name as values for a new variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can accomplish (1) with the code below. Now, I am trying to modify this to extract each file name (_MMDDYYYY) as values for a new variable, date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i);
   proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
      run;
%mend impt;
%let path=D:\Folder\;
data _null_;
   command = "dir &amp;amp;path.*.xls /b";;
   infile dummy pipe filevar=command end=eof truncover;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt(',path,',',i,'));'));
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried including variations of the following code, but no new variable is created.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i);
   proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
      run;
%mend impt;
%let path=D:\Folder\;
data _null_;
&lt;FONT color="#3366FF"&gt;   length fname $200 ;&lt;/FONT&gt;
   command = "dir &amp;amp;path.*.xls /b";;
   infile  dummy pipe filevar=command end=eof truncover &lt;FONT color="#3366FF"&gt;filename=fname&lt;/FONT&gt;;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt(',path,',',i,'));'));
	  &lt;FONT color="#3366FF"&gt;call symputx('fname',fname)&lt;/FONT&gt;;
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I fix this? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 19:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559100#M17104</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T19:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559116#M17106</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54903"&gt;@pamplemousse822&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a folder with ~3000 .xls files. I am trying to (1) import all into sas, and (2) extract each file name as values for a new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can accomplish (1) with the code below. Now, I am trying to modify this to extract each file name (_MMDDYYYY) as values for a new variable, date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i);
   proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
      run;
%mend impt;
%let path=D:\Folder\;
data _null_;
   command = "dir &amp;amp;path.*.xls /b";;
   infile dummy pipe filevar=command end=eof truncover;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt(',path,',',i,'));'));
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried including variations of the following code, but no new variable is created.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i);
   proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
      run;
%mend impt;
%let path=D:\Filder\;
data _null_;
&lt;FONT color="#3366FF"&gt;   length fname $200 ;&lt;/FONT&gt;
   command = "dir &amp;amp;path.*.xls /b";;
   infile  dummy pipe filevar=command end=eof truncover &lt;FONT color="#3366FF"&gt;filename=fname&lt;/FONT&gt;;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt(',path,',',i,'));'));
	  &lt;FONT color="#3366FF"&gt;call symputx('fname',fname)&lt;/FONT&gt;;
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I fix this? Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&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;I think when you use FILENAME=, the variable named (in your case FNAME) is temporary, it disappears when the data step ends. So you have to assign it to a data step variable (for example, called FILENAME) that will be present when the data step ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename=fname;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 19:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559116#M17106</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-15T19:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559121#M17107</link>
      <description>hi, thank you&lt;BR /&gt;&lt;BR /&gt;i moved it to above the infile line, still does not work. other ideas?</description>
      <pubDate>Wed, 15 May 2019 19:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559121#M17107</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T19:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559128#M17108</link>
      <description>&lt;P&gt;Where did you think that you created a new variable?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only data step is a data _NULL_ step which does not create any datasets.&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 19:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559128#M17108</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-15T19:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559130#M17109</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54903"&gt;@pamplemousse822&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;hi, thank you&lt;BR /&gt;&lt;BR /&gt;i moved it to above the infile line, still does not work. other ideas?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Above the INFILE command the variable in FILENAME= has no value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should go after the INFILE command.&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 19:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559130#M17109</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-15T19:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559131#M17110</link>
      <description>All the new datasets are labeled _1 to _3000, I think from the out option in proc import. I don't fully understand how it works (found the code on another sas communities post) but it does import all files into the work directory. now, just need the date.</description>
      <pubDate>Wed, 15 May 2019 19:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559131#M17110</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T19:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559135#M17111</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i);
   proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
      run;
%mend impt;
%let path=D:\Folder\;
data _null_;
   length fname $200 ;
   command = "dir &amp;amp;path.*.xls /b";;
   infile  dummy pipe filevar=command end=eof truncover filename=fname ;;
   filename=fname;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt(',path,',',i,'));'));
	  fname=fname;
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried this, still no new variable. I also removed the filename=fname from infile statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 19:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559135#M17111</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T19:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559136#M17112</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54903"&gt;@pamplemousse822&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;All the new datasets are labeled _1 to _3000, I think from the out option in proc import. I don't fully understand how it works (found the code on another sas communities post) but it does import all files into the work directory. now, just need the date.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;PROC IMPORT is not going to add any variables that aren't in the input file.&lt;/P&gt;
&lt;P&gt;Looks like you want to update the macro to take a new parameter and then use that to add the variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i,fname);
proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
run;
data _&amp;amp;i;
  set _&amp;amp;i;
  length fname $200;
  fname="&amp;amp;fname";
run;
%mend impt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then update the CALL EXECUTE to pass in that new value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call execute(cats('%nrstr(%impt)(',path,',',i,',',fname,');'));
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 May 2019 19:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559136#M17112</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-15T19:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559145#M17113</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you! It is almost working. Now since the actual file path is very long the variable, truncates. I only need the actual filename. How can I fix this? I think it is something that says only take the last 9 characters in the filename (_MMDDYYYY). But I am not sure where to put this in the code.&amp;nbsp; Here is what I have so far.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(filename,i,fname);
proc import DATAFILE="&amp;amp;filename" OUT=_&amp;amp;i DBMS=xls REPLACE;
run;
data _&amp;amp;i;
  set _&amp;amp;i;
  length fname $200;
  fname="&amp;amp;fname";
run;
%mend impt;
%let path=D:\Sindana\GreatRecession_PM\test\;
data _null_;
	length fname $1000 ;
   command = "dir &amp;amp;path.*.xls /b";;
   infile dummy pipe filevar=command end=eof truncover filename=fname;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
      put 'NOTE: ' path=;
      call execute(cats('%nrstr(%impt)(',path,',',i,',',fname,'));'));
      end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 20:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559145#M17113</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T20:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559152#M17114</link>
      <description>&lt;P&gt;Are the names valid names for datasets?&amp;nbsp; If so I would just use the name of the file as the name of the dataset.&lt;/P&gt;
&lt;P&gt;In that case you might not need to actually add the variable to the dataset as you could do that later when you USE the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combine;
  set _01012019 _02012019 indsname=indsname;
  dsname=indsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So something like this. Note that your data step was overly complex for the method you are using to get the filenames.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(fname,path);
proc import DATAFILE="&amp;amp;path" OUT=&amp;amp;fname DBMS=xls REPLACE;
run;
%mend impt;

%let path=D:\xxxx\test\;
data _null_;
  infile "dir &amp;amp;path.*.xls /b" pipe truncover;
  input path $256. ;
  fname = scan(scan(path,-1,'\'),1,'.');
  call execute(cats('%nrstr(%impt)(fname=',fname,',path=',path,');'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might need to make the logic that creates FNAME from the full PATH more complex.&amp;nbsp; The logic above is just&amp;nbsp; taking the part after the last folder name and then taking the part up to the first period.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if&amp;nbsp; path= 'D:\....\_01012019.xls' then fname will be '_01012019'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 20:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559152#M17114</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-15T20:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559181#M17117</link>
      <description>&lt;P&gt;Thank you!!! It worked with the following code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro impt(fname,path);
	proc import DATAFILE="&amp;amp;path" OUT=&amp;amp;fname DBMS=xls REPLACE;
	run;
%mend impt;

%let path=D:\Folder\;
data _null_;
	length fname $100;
   command = "dir &amp;amp;path.*.xls /b";;
   infile dummy pipe filevar=command end=eof truncover filename=fname;
   do i = 1 by 1 while(not eof);
      input path $128.;
      path=catt("&amp;amp;path.",path);
	  fname = scan(scan(path,-1,'\'),1,'.');
      call execute(cats('%nrstr(%impt)(fname=',fname,',path=',path,');'));
      end;
   stop;
run;

data combine;
  set _: indsname=indsname;
  date=substr(indsname, 7); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 May 2019 21:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559181#M17117</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2019-05-15T21:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract variable from filename?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559244#M17122</link>
      <description>&lt;P&gt;The way I tackle this sort of thing, in DI Studio, is to run a UT/Macro that collects all the filenames and paths into a table, then use that table to control a Loop, cycling though the records in the "file listing" table, one at at time, which nicely gives you the current filename as a macro variable, which you pass to the infile statement, and use in the population of the table(s) you create from the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Equally, you can route file to various different "readers" based on assessing their nature by parsing their filenames, assuming there's a structure/schema in their naming (which there should be).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Easy to add the filename each record came from to the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pretty standard to have data/metadata encoded into the filename, and simple to extract it from them, dates, file content types etc.&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2019 09:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-extract-variable-from-filename/m-p/559244#M17122</guid>
      <dc:creator>AngusLooney</dc:creator>
      <dc:date>2019-05-16T09:41:36Z</dc:date>
    </item>
  </channel>
</rss>

