<?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 populate variable with file name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767114#M243161</link>
    <description>&lt;P&gt;is there a way to use the name of the file in the set statement as a variable entry&lt;/P&gt;&lt;P&gt;&amp;nbsp;new_var=name of datasetname in set statement..&lt;/P&gt;&lt;P&gt;Thanks for the documentation you sent. A bit too advanced for me so it is pretty intimidating.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Sep 2021 20:13:05 GMT</pubDate>
    <dc:creator>K_S</dc:creator>
    <dc:date>2021-09-10T20:13:05Z</dc:date>
    <item>
      <title>how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767080#M243140</link>
      <description>&lt;P&gt;I have excel files with single observations (one row + the row with the column titles) come in. The excels all have the same number of columns and variable names - i.e. exact same structure.&lt;/P&gt;&lt;P&gt;Each excel's file name is an ID code.&lt;/P&gt;&lt;P&gt;After importing said excels,&amp;nbsp; I will stack them and build a database. The problem is that the ID code is not contained within the excel itself...it only appears in the file name.&lt;/P&gt;&lt;P&gt;I would like to extract the ID code from the file title and add it as a variable entry.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let say I have an excel that is called 20210123.&amp;nbsp; I would like to grab this number and enter it into the database as the IDCode variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would you go about coding this?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 17:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767080#M243140</guid>
      <dc:creator>K_S</dc:creator>
      <dc:date>2021-09-10T17:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767084#M243145</link>
      <description>&lt;P&gt;1. Get list of excel files&lt;/P&gt;
&lt;P&gt;Now for each file:&lt;/P&gt;
&lt;P&gt;2. Import Excel file&lt;/P&gt;
&lt;P&gt;3. Add column to Excel file with file name&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. Append to master table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Get list of Excel files&lt;/P&gt;
&lt;P&gt;2. Import each Excel file and add name (2/3)&lt;/P&gt;
&lt;P&gt;3. Append all data sets together.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First two macros here can get you started.&lt;/P&gt;
&lt;P&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;DIV class="xis-toc_1"&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Example 1: Import All CSV Files That Exist within a Directory&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV class="xis-toc_1"&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/n0js70lrkxo6uvn1fl4a5aafnlgt.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Example 2: List All Files within a Directory Including Subdirectories&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV class="xis-toc_1"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="xis-toc_1"&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127053"&gt;@K_S&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have excel files with single observations (one row + the row with the column titles) come in. The excels all have the same number of columns and variable names - i.e. exact same structure.&lt;/P&gt;
&lt;P&gt;Each excel's file name is an ID code.&lt;/P&gt;
&lt;P&gt;After importing said excels,&amp;nbsp; I will stack them and build a database. The problem is that the ID code is not contained within the excel itself...it only appears in the file name.&lt;/P&gt;
&lt;P&gt;I would like to extract the ID code from the file title and add it as a variable entry.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let say I have an excel that is called 20210123.&amp;nbsp; I would like to grab this number and enter it into the database as the IDCode variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would you go about coding this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/DIV&gt;
&lt;DIV class="xis-toc_1"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 10 Sep 2021 18:02:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767084#M243145</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-10T18:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767086#M243146</link>
      <description>&lt;P&gt;Do you want to gel something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* test data */

%let folder = %sysfunc(pathname(WORK))/test;

options dlcreatedir;
libname test "&amp;amp;folder.";

%macro genData();
%do i = 1 %to 5;
proc export 
  data = sashelp.class(firstobs=&amp;amp;i. obs = &amp;amp;i.)
  outfile = "&amp;amp;folder./&amp;amp;i..xlsx"
  dbms=xlsx replace
;
run;
%end;
%mend;

%genData()



/* get Excels names */
data listOfFiles;
  base = "&amp;amp;folder.";
  length file $ 256;

  folderRef = "_%sysfunc(datetime(), hex6.)0";

  rc = filename(folderRef, base);
  folderid = dopen(folderRef);

  do i=1 to dnum(folderId); drop i;
    file = dread(folderId, i);
    if upcase(scan(file, -1, ".")) = "XLSX" then output;
  end;

  rc = filename(folderRef);
keep file;
run;
proc sort data = listOfFiles;
  by file;
run;
proc print data = listOfFiles;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 18:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767086#M243146</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-09-10T18:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767114#M243161</link>
      <description>&lt;P&gt;is there a way to use the name of the file in the set statement as a variable entry&lt;/P&gt;&lt;P&gt;&amp;nbsp;new_var=name of datasetname in set statement..&lt;/P&gt;&lt;P&gt;Thanks for the documentation you sent. A bit too advanced for me so it is pretty intimidating.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 20:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767114#M243161</guid>
      <dc:creator>K_S</dc:creator>
      <dc:date>2021-09-10T20:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767118#M243162</link>
      <description>&lt;P&gt;Then go through these ones first.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, it's not a particularly difficult task you're trying to do but it automating something which is a bit of a different skill set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also search on here, I know this question has been asked and answered many times.&amp;nbsp; It depends on if you're trying to solve a problem or if you care if you understand how you solve it to be able to reuse the code in the future.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&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&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 20:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767118#M243162</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-10T20:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to populate variable with file name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767142#M243169</link>
      <description>&lt;P&gt;What types of files are they? Modern XLSX files?&amp;nbsp; Older XLS files?&lt;/P&gt;
&lt;P&gt;Or are they not Excel files at all.&amp;nbsp; Perhaps they are instead CSV files and it is just that your computer has been instructed to open them in Excel by default if not told to do something different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they are CSV files then it is MUCH easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's think about how to deal with a set of actual Excel files, either XLS or XLSX.&amp;nbsp; One way to make a consistent data structure might be to first convert the files into a single CSV file and then read that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First get a list of the files.&amp;nbsp; Easiest way is to use your operating system command for listing files.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files;
  infile 'ls /my-directory/*.xlsx' pipe truncover ;
  input filename $256.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that you have the list of files you can use it to generate code to import each file and write its line(s) of data to a CSV file.&lt;/P&gt;
&lt;P&gt;So first generate the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set files ;
  file code;
  length idcode $30 ;
  idcode = scan(filename,-2,'./\');
  put 'proc import dbms=xlsx out=next replace datafile=' filename :$quote. ';'
    / 'run;'
    / 'data _null_;'
    / '  idcode=' idcode :$quote. ';'
    / '  file csv dsd mod;'
    / '  set next;'
    / '  put (_all_) (+0);'
    / 'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then point CSV at the file you want to use to store the new text file with all of the data and run the generated code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that you have all of the data in the a single CSV file you can read in the data with a data step.&amp;nbsp; That way&amp;nbsp;you can control how each of the variables is defined.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile csv dsd truncover ;
* Define the length and order of all of your variables ;
* remember numeric variables use length 8 because they are 64 bit floating point values;
  length idcode $30 .....  all of your other variables .... ;
  input (_all_) (+0);
  * add any FORMAT or INFORMAT statements you might need ;
  * add any LABEL statements you might need ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 21:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-populate-variable-with-file-name/m-p/767142#M243169</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-10T21:37:18Z</dc:date>
    </item>
  </channel>
</rss>

