<?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 How to rename a variable using filename in macro in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374403#M11357</link>
    <description>&lt;P&gt;Hello friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to rename a variable in macro by using filename (variable name = filename). I use the following code to import files and then use macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* Read datafiles in folder*/&lt;BR /&gt;&lt;BR /&gt;%let subdir=C:\Users\Tri_Tri_nguyen\Desktop\Dead_final\;&lt;BR /&gt;filename dir "&amp;amp;subdir.*.csv ";&lt;BR /&gt;data new;&lt;BR /&gt;length filename fname $ 256;&lt;BR /&gt;infile dir eof=last filename=fname;&lt;BR /&gt;input ;&lt;BR /&gt;last: filename=fname;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/* sort data and remove duplicates-*/&lt;BR /&gt;&lt;BR /&gt;proc sort data=new nodupkey;&lt;BR /&gt;by filename;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data null;&lt;BR /&gt;set new;&lt;BR /&gt;call symputx(cats('filename',_n_),filename);&lt;BR /&gt;call symputx(cats('dsn',_n_),compress(scan(filename,-2,'\.') ,'ka'));&lt;BR /&gt;call symputx('nobs',_n_);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;nobs.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro import;&lt;BR /&gt;%do i=1 %to &amp;amp;nobs;&lt;BR /&gt;&lt;BR /&gt;/* Import for file No. 9 of board of directors */&lt;BR /&gt;&lt;BR /&gt;proc import&lt;BR /&gt;datafile="&amp;amp;&amp;amp;filename&amp;amp;i"&lt;BR /&gt;out=dsn&amp;amp;i&lt;BR /&gt;dbms=csv&lt;BR /&gt;replace;&lt;BR /&gt;getnames=no;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;....&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data Dsn&amp;amp;i; set Dsn&amp;amp;i;&lt;BR /&gt;rename COL1=&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WC&amp;amp;filename&lt;/STRONG&gt;&lt;/FONT&gt;; /&lt;FONT color="#FF0000"&gt;* I want to rename COL1 (in the imported file) using words "WC" and the name of the files I have imported above". For example, the imported file name is 12345, I want to rename COL1 into "wc12345". I got a lot of files like this (that is why I need a macro) */&lt;/FONT&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;...&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend import;&lt;BR /&gt;%import&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything in the macro works well, except rename issue. Thank you very much for your support!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers, Thierry.&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jul 2017 09:20:46 GMT</pubDate>
    <dc:creator>tritringuyen</dc:creator>
    <dc:date>2017-07-10T09:20:46Z</dc:date>
    <item>
      <title>How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374403#M11357</link>
      <description>&lt;P&gt;Hello friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to rename a variable in macro by using filename (variable name = filename). I use the following code to import files and then use macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* Read datafiles in folder*/&lt;BR /&gt;&lt;BR /&gt;%let subdir=C:\Users\Tri_Tri_nguyen\Desktop\Dead_final\;&lt;BR /&gt;filename dir "&amp;amp;subdir.*.csv ";&lt;BR /&gt;data new;&lt;BR /&gt;length filename fname $ 256;&lt;BR /&gt;infile dir eof=last filename=fname;&lt;BR /&gt;input ;&lt;BR /&gt;last: filename=fname;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/* sort data and remove duplicates-*/&lt;BR /&gt;&lt;BR /&gt;proc sort data=new nodupkey;&lt;BR /&gt;by filename;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data null;&lt;BR /&gt;set new;&lt;BR /&gt;call symputx(cats('filename',_n_),filename);&lt;BR /&gt;call symputx(cats('dsn',_n_),compress(scan(filename,-2,'\.') ,'ka'));&lt;BR /&gt;call symputx('nobs',_n_);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;nobs.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro import;&lt;BR /&gt;%do i=1 %to &amp;amp;nobs;&lt;BR /&gt;&lt;BR /&gt;/* Import for file No. 9 of board of directors */&lt;BR /&gt;&lt;BR /&gt;proc import&lt;BR /&gt;datafile="&amp;amp;&amp;amp;filename&amp;amp;i"&lt;BR /&gt;out=dsn&amp;amp;i&lt;BR /&gt;dbms=csv&lt;BR /&gt;replace;&lt;BR /&gt;getnames=no;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;....&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data Dsn&amp;amp;i; set Dsn&amp;amp;i;&lt;BR /&gt;rename COL1=&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WC&amp;amp;filename&lt;/STRONG&gt;&lt;/FONT&gt;; /&lt;FONT color="#FF0000"&gt;* I want to rename COL1 (in the imported file) using words "WC" and the name of the files I have imported above". For example, the imported file name is 12345, I want to rename COL1 into "wc12345". I got a lot of files like this (that is why I need a macro) */&lt;/FONT&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;...&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend import;&lt;BR /&gt;%import&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything in the macro works well, except rename issue. Thank you very much for your support!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers, Thierry.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 09:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374403#M11357</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2017-07-10T09:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374407#M11358</link>
      <description>&lt;P&gt;You are using &amp;amp;filename; why are you not using &amp;amp;&amp;amp;filename&amp;amp;i?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is not the cause of your problem, post the log, and how the result differs from your expectations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 09:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374407#M11358</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-10T09:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374412#M11359</link>
      <description>&lt;P&gt;Thank you very much for your response. The log is follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MPRINT(IMPORT): * Step 4: Rename variables;&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 2&lt;BR /&gt;MPRINT(IMPORT): data Dsn22;&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 2&lt;BR /&gt;MPRINT(IMPORT): set Dsn22;&lt;BR /&gt;SYMBOLGEN: &amp;amp;&amp;amp; resolves to &amp;amp;.&lt;BR /&gt;SYMBOLGEN: &amp;amp;&amp;amp; resolves to &amp;amp;.&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 2&lt;BR /&gt;SYMBOLGEN: Macro variable FILENAME2 resolves to&lt;BR /&gt;C:\Users\Tri_Tri_nguyen\Desktop\Dead_final\01100.csv&lt;BR /&gt;NOTE: Line generated by the macro variable "FILENAME2".&lt;BR /&gt;1763 WCC:\Users\Tri_Tri_nguyen\Desktop\Dead_final\01100.csv&lt;BR /&gt;-&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;MPRINT(IMPORT): rename&lt;BR /&gt;COL1=WCC:\Users\Tri_Tri_nguyen\Desktop\Dead_final\01100.csv;&lt;BR /&gt;MPRINT(IMPORT): run;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, ;.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;ERROR 76-322: Syntax error, &lt;/FONT&gt;statement&lt;FONT color="#FF0000"&gt; will be ignored.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.DSN22 may be incomplete. When this step was&lt;BR /&gt;stopped there were 0 observations and 7 variables.&lt;BR /&gt;WARNING: Data set WORK.DSN22 was not replaced because this step was stopped.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, the variables are not renamed in the macro. I want to merge datasets after import and do some modifications. It is essential to rename a variable using filename. Please have a look at them and give&amp;nbsp;me some advice. Thank you very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers, Thierry&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 09:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374412#M11359</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2017-07-10T09:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374413#M11360</link>
      <description>&lt;P&gt;Also, the name of datasets in my actual SAS code is&amp;nbsp;Dsn2&amp;amp;i (not&amp;nbsp;Dsn&amp;amp;i as stated in the question above).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 09:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374413#M11360</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2017-07-10T09:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374414#M11361</link>
      <description>&lt;P&gt;C:\Users\Tri_Tri_nguyen\Desktop\Dead_final\01100.csv&lt;/P&gt;
&lt;P&gt;is NOT a valid SAS name.&lt;/P&gt;
&lt;P&gt;SAS names consist of the 26 letters of the standard alphabet, digits, or underlines, and must not start with a digit.&lt;/P&gt;
&lt;P&gt;I suggest you build a valid SAS name from the name of your infile, like&lt;/P&gt;
&lt;P&gt;file_01100&lt;/P&gt;
&lt;P&gt;where 01100 is the part of the filename between the final backslash and the dot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 09:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374414#M11361</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-10T09:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374421#M11362</link>
      <description>Yes, that is exactly what I want. I only want a variable name 'wc01100', where 01100 is the last part of file name. If so, how I should modify sas code? Again, it is in a macro.&lt;BR /&gt;&lt;BR /&gt;Thanks, Thierry</description>
      <pubDate>Mon, 10 Jul 2017 10:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374421#M11362</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2017-07-10T10:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374447#M11363</link>
      <description>&lt;P&gt;Extract the important part with scan() functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let filename2=C:\Users\Tri_Tri_nguyen\Desktop\Dead_final\01100.csv;

data _null_;
whole_path = "&amp;amp;filename2";
col1_name = 'wc' !! scan(scan(whole_path,-1,'\'),1,'.');
call symput('col1_name',trim(col1_name));
run;

%put &amp;amp;col1_name.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use &amp;amp;col1_name. in the rename statement.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 11:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/374447#M11363</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-10T11:38:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to rename a variable using filename in macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/376611#M11464</link>
      <description>&lt;P&gt;Thank you very much! It does not work. However, I may try rename manually. Thank you very much!&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 15:26:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-rename-a-variable-using-filename-in-macro/m-p/376611#M11464</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2017-07-17T15:26:37Z</dc:date>
    </item>
  </channel>
</rss>

