<?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: Proc SQL into: macro variable not resolving inside a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406423#M98964</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168776"&gt;@constliv&lt;/a&gt; wrote:&lt;BR /&gt;Hi Tom,&lt;BR /&gt;&lt;BR /&gt;You may want to to edit the coalesce part so that tehcnically the code is indeed the final solution.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Updated.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Oct 2017 06:32:23 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-10-23T06:32:23Z</dc:date>
    <item>
      <title>Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406375#M98943</link>
      <description>&lt;P&gt;I have the following listing of dataset names and thier dataset labels in a dataset callef "listfiles" on SAS EG v 5.1 :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;memname&lt;/TD&gt;&lt;TD&gt;memlabel&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CHECKING&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LISTFILES&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NOV&lt;/TD&gt;&lt;TD&gt;Not sorted data&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NOV_1&lt;/TD&gt;&lt;TD&gt;Nov_1 dataset that is sorted&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SORTED_NOV&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;T&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;_PRODSAVAIL&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The formats of both columns are length of 32 bytes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing a macro to retrieve the dataset label giving the dataset name as a parameter, for the purpose of using the dataset lable as title in proc print. I wrote the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options symbolgen ;

%macro label(memname=) ;
%global &amp;amp;titleone   ;
proc sql;
select trim(memlabel) into :"&amp;amp;titleone." length = 256.
from listfiles 
where trim(memname) = &amp;amp;memname ;
quit;
%mend label ;

%label (memname= "NOV" ) ;

%put  &amp;amp;titleone ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I keep getting the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable TITLEONE resolves to Not sorted data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable TITLEONE resolves to Not sorted data&lt;/P&gt;&lt;P&gt;&amp;nbsp;NOTE: Line generated by the macro variable "TITLEONE".&lt;/P&gt;&lt;P&gt;32 "Not sorted data&lt;/P&gt;&lt;P&gt;_________________________________________________________________________________________________________________________&lt;/P&gt;&lt;P&gt;22&lt;/P&gt;&lt;P&gt;76&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable MEMNAME resolves to "NOV"&lt;/P&gt;&lt;P&gt;ERROR 22-322: Expecting a name.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate help on fixing this. I think it must be something to do with macro quoting....but I am not able to nail it.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2017 15:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406375#M98943</guid>
      <dc:creator>constliv</dc:creator>
      <dc:date>2017-10-22T15:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406379#M98944</link>
      <description>&lt;P&gt;In the into-clause in proc sql you have to use the name of the macro variable without ampersand and quotes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
options symbolgen ;

%macro label(memname=) ;
  %global &amp;amp;titleone   ;

  proc sql noprint;
    select trim(memlabel) into :titleone trimmed
    from listfiles 
    where trim(memname) = "&amp;amp;memname";
  quit;
%mend label ;

%label (memname= NOV ) ;

%put  &amp;amp;titleone ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Oct 2017 15:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406379#M98944</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2017-10-22T15:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406387#M98950</link>
      <description>&lt;P&gt;Is the name of the macro variable you want to populate TITLEONE? Or does the macro variable named TITLEONE contain the name that you want to use for the macro variable that you are going to create?&amp;nbsp; I would assume that you want the former so you need to change the %GLOBAL statement to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global titleone;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then in the SELECT statement you will also just use the name of the variable.&lt;/P&gt;
&lt;P&gt;To trim the results of the select when writing into the macro variable use the TRIMMED keyword of the INTO syntax .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select memlabel into :titleone trimmed&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In your WHERE clause&amp;nbsp; to compare your dataset variable to a character literal. Normally you do NOT include quotes in macro variable values so you should add the quotes when they are needed where you are using the macro variable's value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where memname = "&amp;amp;memname"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also might want to make sure to account for case issue.&amp;nbsp; What if the user used Nov instead of NOV? In SAS code like the DATA= option on PROC PRINT the case wouldn't matter.&amp;nbsp; But when comparing character strings "Nov" and "NOV" are different values.&lt;/P&gt;
&lt;P&gt;What do you want TITLEONE to have when the member name is not found?&lt;/P&gt;
&lt;P&gt;Also what did you want to use for those members that do not have labels?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably want something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro label(memname=) ;
%global titleone ;
%let titleone=&amp;amp;memname ;
proc sql noprint;
select coalesce(memlabel,memname) into :titleone trimmed
  from listfiles
  where upcase(memname) = %upcase("&amp;amp;memname")
;
quit;
%mend label ;

%let dsname=nov;
%label (memname= &amp;amp;dsname) ;
Title1 "PROC PRINT for &amp;amp;titleone";
proc print data=&amp;amp;dsname ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 06:30:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406387#M98950</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-23T06:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406419#M98961</link>
      <description>&lt;P&gt;Hi andreas_ids,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution worked. Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And when I extended your solution to print titles, which is the goal of the program, I found that the below code works :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options symbolgen ;

%macro label(memname=) ;
  %global &amp;amp;titleone   ;

  proc sql noprint;
    select trim(memlabel) into :titleone trimmed
    from listfiles 
    where trim(memname) = "&amp;amp;memname";
  quit;
%mend label ;

%label (memname= NOV ) ;

title 'PROC PRINT for' &amp;amp;titleone  ;
proc print data = nov ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Not sure if the community board allows two responses to be selected as 'Accepted as solution' as I would like to accept Tom's solution as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for taking time to help. Much appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 06:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406419#M98961</guid>
      <dc:creator>constliv</dc:creator>
      <dc:date>2017-10-23T06:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406420#M98962</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Well done ! Thank you for the detailed response, which is so helpful and I am sure other readers will also benefit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I liked the defensive programming aspects you brought into the solution and how you made the name of the dataset also a macro variable. Good learning points. Speaking from the other side though, sometimes managers want quick and dirty results and programmers are forced to skip features that will give robust results for want of time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One small thing. I had to tweak the coalesce part of the code to print the dataset label as title if populated, the order of the arguments of the coalesce function had to be swapped as follows (memlabel first and then memname). Otherwise it was not picking up the dataset label as titile for the print output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro label_1(memname=) ;
%global titleone ;
%let titleone=&amp;amp;memname ;
proc sql noprint;
select coalesce(memlabel, memname) into :titleone trimmed
  from listfiles
  where upcase(memname) = %upcase("&amp;amp;memname")
;
quit;
%mend label_1 ;

%let dsname=nov_1;
%label_1 (memname= &amp;amp;dsname) ;
%put &amp;amp;titleone ;
Title1 "PROC PRINT for &amp;amp;titleone";
proc print data=&amp;amp;dsname ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 06:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406420#M98962</guid>
      <dc:creator>constliv</dc:creator>
      <dc:date>2017-10-23T06:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406421#M98963</link>
      <description>Hi Tom,&lt;BR /&gt;&lt;BR /&gt;You may want to to edit the coalesce part so that tehcnically the code is indeed the final solution.</description>
      <pubDate>Mon, 23 Oct 2017 06:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406421#M98963</guid>
      <dc:creator>constliv</dc:creator>
      <dc:date>2017-10-23T06:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL into: macro variable not resolving inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406423#M98964</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168776"&gt;@constliv&lt;/a&gt; wrote:&lt;BR /&gt;Hi Tom,&lt;BR /&gt;&lt;BR /&gt;You may want to to edit the coalesce part so that tehcnically the code is indeed the final solution.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Updated.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 06:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-into-macro-variable-not-resolving-inside-a-macro/m-p/406423#M98964</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-23T06:32:23Z</dc:date>
    </item>
  </channel>
</rss>

