<?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: use sql to get macro variable as filename in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71360#M15447</link>
    <description>thanks a lot</description>
    <pubDate>Wed, 28 Jan 2009 01:38:08 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-01-28T01:38:08Z</dc:date>
    <item>
      <title>use sql to get macro variable as filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71358#M15445</link>
      <description>i use sql to get a macro variable as the name of a file, then want to read this file, but found problems&lt;BR /&gt;
&lt;BR /&gt;
two programs are as follows; is there any diffence btw them&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
first,&lt;BR /&gt;
&lt;BR /&gt;
PROC IMPORT OUT= WORK.t &lt;BR /&gt;
            DATAFILE= "C:\Users\rong\Documents\high-frequency\2006\20061&lt;BR /&gt;
1\200611sh_csv\sh\20061101\SH000001.csv" &lt;BR /&gt;
            DBMS=CSV REPLACE;&lt;BR /&gt;
     GETNAMES=YES;&lt;BR /&gt;
     DATAROW=2; &lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
second&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select  code&lt;BR /&gt;
    into: wkbnm&lt;BR /&gt;
	from jj&lt;BR /&gt;
	where row=1;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;wkbnm;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
PROC IMPORT OUT= WORK.t &lt;BR /&gt;
            DATAFILE= "C:\Users\rong\Documents\high-frequency\2006\20061&lt;BR /&gt;
1\200611sh_csv\sh\20061101\&amp;amp;wkbnm..csv" &lt;BR /&gt;
            DBMS=CSV REPLACE;&lt;BR /&gt;
     GETNAMES=YES;&lt;BR /&gt;
     DATAROW=2; &lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
jj is as follows&lt;BR /&gt;
code                 row&lt;BR /&gt;
SH000001          1&lt;BR /&gt;
SH000002           2&lt;BR /&gt;
SH000003          3</description>
      <pubDate>Tue, 27 Jan 2009 05:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71358#M15445</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-27T05:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: use sql to get macro variable as filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71359#M15446</link>
      <description>Hi:&lt;BR /&gt;
  You could try turning on the SYMBOLGEN option to see how &amp;amp;wkbnm is being resolved. Depending on the length of &amp;amp;wkbnm in the work.jj dataset, you could be generating a name that looks like this after macro variable resolution:&lt;BR /&gt;
[pre]&lt;BR /&gt;
SH000001    .csv   (with trailing spaces)&lt;BR /&gt;
instead of this:&lt;BR /&gt;
SH000001.csv    (without trailing spaces)&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Trailing spaces or blanks are not generally stripped from macro variables when you create them with PROC SQL. SYMBOLGEN can help you figure out what's happening. You can also figure out whether this is what's happening in your case with a simple test. Create this new &amp;amp;FNAME macro variables AFTER your PROC SQL step (note that the PROC SQL step ends with a QUIT and not with a RUN). If you want your macro variables available after the step runs, then you must have a "good" step boundary to the SQL step.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select code&lt;BR /&gt;
    into: wkbnm&lt;BR /&gt;
  from jj&lt;BR /&gt;
  where row=1;&lt;BR /&gt;
quit;&lt;BR /&gt;
                                   &lt;BR /&gt;
%let fname = &amp;amp;wkbnm..csv;&lt;BR /&gt;
%put &amp;amp;fname;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
 If &amp;amp;FNAME displays as &lt;BR /&gt;
[pre]SH000001    .csv[/pre]&lt;BR /&gt;
              &lt;BR /&gt;
then you know that you need to trim the trailing blanks from the macro variable value. (even one space before the ".csv" would result in an incorrect filename reference for your PROC IMPORT syntax.&lt;BR /&gt;
 &lt;BR /&gt;
Trimming the trailing spaces can be done VERY easily, by issuing a %let after the QUIT; statement for the SQL step:&lt;BR /&gt;
                 &lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select code&lt;BR /&gt;
    into: wkbnm&lt;BR /&gt;
  from jj&lt;BR /&gt;
  where row=1;&lt;BR /&gt;
quit;&lt;BR /&gt;
                                    &lt;BR /&gt;
%let wkbnm = &amp;amp;wkbnm;&lt;BR /&gt;
%let fname = &amp;amp;wkbnm..csv;&lt;BR /&gt;
%put &amp;amp;fname;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                    &lt;BR /&gt;
Note the inclusion of the extra %LET statement -- macro variable values assigned with a %LET have the leading and trailing blanks removed from the variable value.&lt;BR /&gt;
    &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 27 Jan 2009 05:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71359#M15446</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-01-27T05:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: use sql to get macro variable as filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71360#M15447</link>
      <description>thanks a lot</description>
      <pubDate>Wed, 28 Jan 2009 01:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-sql-to-get-macro-variable-as-filename/m-p/71360#M15447</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-28T01:38:08Z</dc:date>
    </item>
  </channel>
</rss>

