<?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 Using a variable in PROC SQL AS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887576#M350652</link>
    <description>&lt;P&gt;Howdy,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I'm trying to use a variable in the AS part of my SQL Statement but not having any luck.&amp;nbsp; Here is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;&lt;BR /&gt;&lt;BR /&gt;	CREATE TABLE newtbl AS&lt;BR /&gt;		SELECT 1 AS 'Year Is (&amp;amp;REPORT_YEAR.)'N&lt;BR /&gt;			FROM SomeTable&lt;BR /&gt;&lt;BR /&gt;;QUIT;&lt;/PRE&gt;&lt;P&gt;Does anyone know how to get the variable to render its value in the column name?&lt;/P&gt;</description>
    <pubDate>Wed, 02 Aug 2023 20:42:14 GMT</pubDate>
    <dc:creator>Randy2101</dc:creator>
    <dc:date>2023-08-02T20:42:14Z</dc:date>
    <item>
      <title>Using a variable in PROC SQL AS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887576#M350652</link>
      <description>&lt;P&gt;Howdy,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I'm trying to use a variable in the AS part of my SQL Statement but not having any luck.&amp;nbsp; Here is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;&lt;BR /&gt;&lt;BR /&gt;	CREATE TABLE newtbl AS&lt;BR /&gt;		SELECT 1 AS 'Year Is (&amp;amp;REPORT_YEAR.)'N&lt;BR /&gt;			FROM SomeTable&lt;BR /&gt;&lt;BR /&gt;;QUIT;&lt;/PRE&gt;&lt;P&gt;Does anyone know how to get the variable to render its value in the column name?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2023 20:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887576#M350652</guid>
      <dc:creator>Randy2101</dc:creator>
      <dc:date>2023-08-02T20:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable in PROC SQL AS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887582#M350654</link>
      <description>&lt;P&gt;You cannot use a VARIABLE to make a variable name.&lt;/P&gt;
&lt;P&gt;You could use a SYMBOL (aka a macro variable).&amp;nbsp; So if you have the value in a variable then first put it into a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the macro processor will ignore strings inside of single quotes.&lt;/P&gt;
&lt;P&gt;If the value of the macro variable REPORT_YEAR is already a valid SAS name (letter,digits and underscore and does not start with a digit) then just use it directly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let report_year=2023;
proc sql;
create table newtbl as
  select 1 as Year_is_&amp;amp;report_year.
       , *
  from sometable
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you like that string with the spaces and () then perhaps you can use it as the LABEL on the variable instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let report_year=2023;
proc sql;
create table newtbl as
  select 1 as report_year  label="Year is (&amp;amp;report_year.)"
       , *
  from sometable
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really what to make the dataset hard to work with by using non SAS names for the variable name then use double quotes to make the name literal.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let report_year=2023;
proc sql;
create table newtbl as
  select 1 as "Year is (&amp;amp;report_year.)"n
       , *
  from sometable
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;That invalid name will not work unless you have set the system option VALIDVARNAME to ANY instead of the normal V7 setting.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2023 20:53:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887582#M350654</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-02T20:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable in PROC SQL AS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887646#M350682</link>
      <description>&lt;P&gt;Why do you want to use a label as variable name?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 10:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-variable-in-PROC-SQL-AS/m-p/887646#M350682</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-08-03T10:19:40Z</dc:date>
    </item>
  </channel>
</rss>

