<?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 Call symputx macro variable cannot be used in %if %then? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355884#M83375</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var=x;
proc sql;
	create table detect as select type
	from Sashelp.Vcolumn
	where libname='WORK' and name="&amp;amp;Var.";
quit;
%if &amp;amp;fm='num' %then %do;
.
.
.
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Above is a part of my macro program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure why the condition (%if...%then %do) cannot work...&lt;/P&gt;</description>
    <pubDate>Thu, 04 May 2017 08:34:26 GMT</pubDate>
    <dc:creator>JohnChen_TW</dc:creator>
    <dc:date>2017-05-04T08:34:26Z</dc:date>
    <item>
      <title>Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355884#M83375</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var=x;
proc sql;
	create table detect as select type
	from Sashelp.Vcolumn
	where libname='WORK' and name="&amp;amp;Var.";
quit;
%if &amp;amp;fm='num' %then %do;
.
.
.
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Above is a part of my macro program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure why the condition (%if...%then %do) cannot work...&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 08:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355884#M83375</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-04T08:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355888#M83378</link>
      <description>&lt;P&gt;For statements with a leading % these are macro &amp;nbsp;statements and can only be used in a macro block, i.e.&lt;/P&gt;
&lt;PRE&gt;%macro Tmp ();

  %if ...;

%mend Tmp;

%tmp;
&lt;/PRE&gt;
&lt;P&gt;These cannot be used in open Base SAS code. &amp;nbsp;You could of course just do:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and NAME="X"));
  if type="num" then call execute('%number_version;');
  else call execute('%character_version;');
run;&lt;/PRE&gt;
&lt;P&gt;Just an example, depends verty much on what you are trying to do - which I can't see. &amp;nbsp;Just bear in mind that macro is never&amp;nbsp;&lt;STRONG&gt;needed&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 08:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355888#M83378</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-04T08:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355892#M83380</link>
      <description>&lt;P&gt;There's absolutely nothing to say from this code snippet.&lt;/P&gt;
&lt;P&gt;- is there a macro definition or not?&lt;/P&gt;
&lt;P&gt;- where does &amp;amp;fm come from?&lt;/P&gt;
&lt;P&gt;- what are you trying to do in the %do ... %end block?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; already said, there's a close to 100% chance that what you want to do does not need a macro at all, so tell us what issue you are trying to solve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: PS Your subject mentions call symputx, but there's no call symput or symputx at all in your code.&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 09:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355892#M83380</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-04T09:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355896#M83381</link>
      <description>&lt;P&gt;Sorry, please see the following full code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(have,var);
proc sql;
create table detect as select type
from sashelp.column
where libname='WORK' and name="&amp;amp;var";
quit;

data _null_; set detect;
call symputx('ty',type);
run;

%if &amp;amp;ty='num' %then %do;
proc means data=&amp;amp;have;
var=&amp;amp;var;
run;
%end;

%mend;
%test(x,y);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 May 2017 09:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355896#M83381</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-04T09:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355899#M83382</link>
      <description>&lt;P&gt;And what does dataset x look like, does it have a variable Y? &amp;nbsp;What I suspect is happening is that the table detect is not getting any records based on WORK.X for variable Y and so the macro variable is not being created. &amp;nbsp;Then its failing.&lt;/P&gt;
&lt;P&gt;However this whole piece of code is not needed anyways (as macro is &lt;STRONG&gt;never&lt;/STRONG&gt; needed):&lt;/P&gt;
&lt;PRE&gt;proc means data=have;  
  var _numeric_;
run;&lt;/PRE&gt;
&lt;P&gt;Will do all numeric variables in the dataset, you can filter that output by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc means data=have;  
  var _numeric_;&lt;BR /&gt;  output out=want;
run;&lt;BR /&gt;data want;&lt;BR /&gt;  set want;&lt;BR /&gt;  where ...;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 09:33:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355899#M83382</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-04T09:33:20Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355909#M83386</link>
      <description>&lt;P&gt;Before creating table detect, there was a DATA PROCESS to put the external dataset in WORK named as X.&lt;/P&gt;&lt;P&gt;However, that will be&lt;SPAN&gt;&amp;nbsp;time-consuming to&lt;/SPAN&gt; check the variable types one by one when there are too many datasets, Also, when I put the dataset in WORK, sashelp.Vcolumn&amp;nbsp;has recorded all types of the variables from each dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I think I shoud do PROC SQL to get the type of the variable and save it as a macro variable first, and then do PROC MEANS/PROC FREQ under the condition as %if (type is num/char) %then...%do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;JC&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 10:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355909#M83386</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-04T10:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355912#M83388</link>
      <description>&lt;P&gt;Sorry, one of us is misunderstandingsomething here. &amp;nbsp;Your problem is caused by your dataset not being present, or not having a variable Y, whichever code method you use is not going to change that. &amp;nbsp;As for the code I presented, it works on all numeric variables, you don't need to check the type beforehand which is what your doing now. &amp;nbsp;If you only want one variable, then retain that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc means data=have (keep=y);  
  var _numeric_;&lt;BR /&gt;  output out=want;
run;&lt;/PRE&gt;
&lt;P&gt;Is there any value in adding lots of extra code?&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 10:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355912#M83388</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-04T10:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355916#M83390</link>
      <description>&lt;P&gt;Your comparison is false:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;ty='num' %then %do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro variables do not require quotes to make comparisons. &amp;nbsp;You are actually comparing &amp;amp;ty to 5 characters (including the quotes), not comparing it to 3 characters. &amp;nbsp;The correct comparison would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;ty=num %then %do;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 10:43:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355916#M83390</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-04T10:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355917#M83391</link>
      <description>&lt;P&gt;Let's look at a small piece:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_; set detect;
call symputx('ty',type);
run;

%if &amp;amp;ty='num' %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here you have to keep in mind that the macro language knows only one datatype, namely text. Therefore quotes around text are not needed; if you use them, they will become &lt;U&gt;part of the value&lt;/U&gt;.&lt;/P&gt;
&lt;P&gt;Macro variable ty will therefore contain num or char (without quotes), as assigned in the preceding data step. The comparison with 'num' will therefore always fail. 'num' ne num.&lt;/P&gt;
&lt;P&gt;To make your code (which I hope is for learning only, doing that in real programming would constitute a major programming crime) workable, adapt it like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
run;

%macro test(have,var);

%let ty=x; /* create ty with dummy value to avoid error message */

proc sql noprint;
select type into :ty
from dictionary.columns
where libname = 'WORK' and upcase(memname) = upcase("&amp;amp;have") and upcase(name) = upcase("&amp;amp;var");
quit;

%if &amp;amp;ty = num %then %do;
proc means data=&amp;amp;have;
var &amp;amp;var;
run;
%end;
%else %put Variable not found or variable not numeric;
%mend;

%test(class,age);
%test(class,name);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 May 2017 10:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355917#M83391</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-04T10:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355925#M83393</link>
      <description>Yes, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; said, the program works if removed the quotes.&lt;BR /&gt;&lt;BR /&gt;Many thanks for your explanation and provide.&lt;BR /&gt;You have added to my knowledge.&lt;BR /&gt;&lt;BR /&gt;But why "%let ty=x;" can avoid error message? what kind of error message?&lt;BR /&gt;&lt;BR /&gt;JC</description>
      <pubDate>Thu, 04 May 2017 11:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355925#M83393</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-04T11:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355927#M83394</link>
      <description>&lt;P&gt;If the proc sql finds no matching observations, macro variable ty will not be created in the SQL step. You will then get a WARNING when you use &amp;amp;ty later on. Setting a default value prevents the WARNING.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See Maxims 24&amp;amp;25.&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2017 11:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/355927#M83394</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-04T11:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/356256#M83523</link>
      <description>Thanks for your suggestion, it works.</description>
      <pubDate>Fri, 05 May 2017 07:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/356256#M83523</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-05T07:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Call symputx macro variable cannot be used in %if %then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/356257#M83524</link>
      <description>Okay, thanks for your information!&lt;BR /&gt;Also thanks for all your help!! (@RW9, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;)&lt;BR /&gt;&lt;BR /&gt;JC</description>
      <pubDate>Fri, 05 May 2017 07:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-symputx-macro-variable-cannot-be-used-in-if-then/m-p/356257#M83524</guid>
      <dc:creator>JohnChen_TW</dc:creator>
      <dc:date>2017-05-05T07:24:25Z</dc:date>
    </item>
  </channel>
</rss>

