<?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 use filenames to create variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724643#M224984</link>
    <description>&lt;P&gt;Much harder to do with EXCEL files than it would be with text files.&amp;nbsp; When reading text files you can use the FILENAME= option on the INFILE statement to access the name of the file being read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need to use macro variable(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fname=1234A_file.xls;
proc import datafile="&amp;amp;fname" out=step1 replace dbsm=xls ;
run;
data want ;
   var1 = "%substr(&amp;amp;fname,1,5)";
   var2 = substr(var1,5,1);
   set step1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 08 Mar 2021 20:38:23 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-03-08T20:38:23Z</dc:date>
    <item>
      <title>How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724618#M224972</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a few hundred filenames that look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1234A_file.xls&lt;/P&gt;&lt;P&gt;ABCDB_file.xls&lt;/P&gt;&lt;P&gt;CATSA_file.xls&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to take the first four characters to create a variable and the fifth character to create a separate variables at the beginning of my dataset. The dataset would look something like what is attached.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would greatly appreciate any guidance on how to achieve this.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 19:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724618#M224972</guid>
      <dc:creator>marksanter</dc:creator>
      <dc:date>2021-03-08T19:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724625#M224975</link>
      <description>From the variable values that's straightforward.&lt;BR /&gt;&lt;BR /&gt;var1 = substr(variable, 1, 4);&lt;BR /&gt;var2 = substr(variable, 5, 1);&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 08 Mar 2021 20:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724625#M224975</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-08T20:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724627#M224976</link>
      <description>&lt;P&gt;Thank you for your help, will this pull the variable names from the file name itself? The current spreadsheets do not include the variables I need - those are found in the file names.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Mark&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 20:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724627#M224976</guid>
      <dc:creator>marksanter</dc:creator>
      <dc:date>2021-03-08T20:11:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724634#M224980</link>
      <description>&lt;P&gt;I would assume you're capturing the filenames when importing your data. If you're not, then that is a different question and the answer depends on how you're importing your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think you explained your problem fully...&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 20:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724634#M224980</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-08T20:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724643#M224984</link>
      <description>&lt;P&gt;Much harder to do with EXCEL files than it would be with text files.&amp;nbsp; When reading text files you can use the FILENAME= option on the INFILE statement to access the name of the file being read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need to use macro variable(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fname=1234A_file.xls;
proc import datafile="&amp;amp;fname" out=step1 replace dbsm=xls ;
run;
data want ;
   var1 = "%substr(&amp;amp;fname,1,5)";
   var2 = substr(var1,5,1);
   set step1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Mar 2021 20:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724643#M224984</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-08T20:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to use filenames to create variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724653#M224989</link>
      <description>&lt;P&gt;Not sure if this is what you are looking for. The code below goes out and grabs all the files in a directory and creates variable from the file names. I included a link to the External File Functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*

External File Functions
https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=allprodsle&amp;amp;docsetTarget=syntaxByCategory-function.htm&amp;amp;locale=en#n1xjxmfjlyclzjn1hyatp8m76hr7

*/

filename myDir "\Some\folder\some\where" ;

data want ;
 	/* get a directory handle for the directory */
	dirRC=dopen("myDir") ;
	put dirRC= ;
	/* get the number of files in the directory */
	dNum=dnum(dirRC) ;
	put dNum= ;
	/* loop through the files */
	do i=1 to dnum ;
		/* get the filename */
		filename=dread(dirRC,i) ;
		put filename= ;
		/* now do whatever you want with the filename */
		var1=substr(filename,1,4) ;
		output ;
	end ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Mar 2021 20:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-filenames-to-create-variables/m-p/724653#M224989</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-03-08T20:55:34Z</dc:date>
    </item>
  </channel>
</rss>

