<?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: Passing the logic for a WHERE condition in a PROC SQL using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508943#M136744</link>
    <description>Are you sure that first one works? It's not working for me.</description>
    <pubDate>Tue, 30 Oct 2018 21:34:33 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-10-30T21:34:33Z</dc:date>
    <item>
      <title>Passing the logic for a WHERE condition in a PROC SQL using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508939#M136740</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a SAS data set where I have 100 variables and 200k observations. For each variable, I need to get the count of all rows that pass a specific condition. Mainly, checking the information quality for each variable. For simplicity purposes, I have created a simple data set with 3 variables (all are characters variables) and 6 observations. 3 variables are COMID_NEW, COMNAME and COMLINK.&lt;/P&gt;
&lt;P&gt;A&amp;nbsp;simple SAS data set, is in the attached document.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the SAS code that I am passing through a macro –&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Code 1:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; comchk(var=, cndtn=);&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table &amp;amp;var as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select count(&amp;amp;var) as &amp;amp;var&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from test_data2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where compress(strip(&amp;amp;var), &amp;amp;cndtn., "ki") = ' '&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; order by &lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data = &amp;amp;var out=&amp;amp;var._t(rename=(_name_=name col1=fmt_check));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data &amp;amp;var._t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;var._t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; format name $200.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comid_new, cndtn = "ABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&amp;amp;*()-_=+\|[]{};:',.&amp;lt;&amp;gt;?/ ");&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comname, cndtn = "0123456789`~!@#$%^&amp;amp;*()-_=+\|[]{};:',.&amp;lt;&amp;gt;?/ ");&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comlink, cndtn = "0123456789`~!@#$%^&amp;amp;*()-_=+\|[]{};:',&amp;lt;&amp;gt;?/ ");&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code works fine without any issue. However, what I want to achieve is, be able to pass the entire logic (in the WHERE statement) to CNDTN macro variable.&lt;/P&gt;
&lt;P&gt;For e.g., something like what I am doing in the code below-&lt;/P&gt;
&lt;P&gt;However, this fails and there is something that I am not doing right.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Could you please help me understand why is my 2&lt;SUP&gt;nd&lt;/SUP&gt; code (as below) failing?? THANKS a ton!!&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Again, my question is not about how to make my code effective (suggestions are very welcome!), however my main question is how can I pass a full logic along with functions to a macro variable?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Code 2:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; comchk(var=, cndtn=);&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table &amp;amp;var as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select count(&amp;amp;var) as &amp;amp;var&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from test_data2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where &amp;amp;cndtn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; order by &lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data = &amp;amp;var out=&amp;amp;var._t(rename=(_name_=name col1=fmt_check));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data &amp;amp;var._t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;var._t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; format name $200.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comid_new, cndtn = (compress(strip(comid_new), "ABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&amp;amp;*()-_=+\|[]{};:',.&amp;lt;&amp;gt;?/ ", "ki")=' ');&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comname, cndtn = (compress(strip(comname) = "mycompany");&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;comchk&lt;/EM&gt;&lt;/STRONG&gt;(var=comlink, cndtn = (compress(strip(comlink), "0123456789`~!@#$%^&amp;amp;*()-_=+\|[]{};:',&amp;lt;&amp;gt;?/ ", "ki")=' ');&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 21:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508939#M136740</guid>
      <dc:creator>path2success</dc:creator>
      <dc:date>2018-10-30T21:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Passing the logic for a WHERE condition in a PROC SQL using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508943#M136744</link>
      <description>Are you sure that first one works? It's not working for me.</description>
      <pubDate>Tue, 30 Oct 2018 21:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508943#M136744</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-30T21:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Passing the logic for a WHERE condition in a PROC SQL using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508966#M136751</link>
      <description>&lt;P&gt;Instead of creating hundreds of tables why not try 1 table and use a better procedure to count.&lt;/P&gt;
&lt;P&gt;The below adds one line an one variable to a new version of the data set and provides a label.&lt;/P&gt;
&lt;P&gt;The label could be more descriptive if you have multiple tests involving the same variable.&lt;/P&gt;
&lt;P&gt;This approach has a couple of other advantages: you are much less likely to have issues with passing values as macro parameters,&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Much&lt;/STRONG&gt; easier to write code that will test multiple ranges, referential values (dependent on another variable or two), and a whole lot more compact code to begin with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then Proc Freq counts 0 and 1. The 1 are true.&lt;/P&gt;
&lt;P&gt;Or use a format to show True/False or Yes/no for the 1/0 values.&lt;/P&gt;
&lt;PRE&gt;data example;
   set sashelp.class;
   condition1= (sex='F');
   condition2= (height &amp;gt; 60);
   condition3= (weight &amp;lt; 120);
   label 
   condition1= 'Sex condition true'
   condition2= 'Height condition true'
   condition3= 'Weight condition true'
   ;
run;

proc freq data=example;
   tables condition: ;
run;&lt;/PRE&gt;
&lt;P&gt;Or a more compact report&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc tabulate data=example;
   var condition: ;
   table condition: ,
         sum='Count'*f=best6.
   ;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you haven't seen the : list that says basically "place all variables whose names start with this string in to the list here".&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 22:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508966#M136751</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-30T22:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: Passing the logic for a WHERE condition in a PROC SQL using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508967#M136752</link>
      <description>&lt;P&gt;Unbalanced parentheses?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro comchk( cndtn );

proc print data =SASHELP.CLASS;
  where ( &amp;amp;cndtn. );
run;
 
%mend;
 
%comchk( cndtn = compress(strip(NAME), "ABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&amp;amp;*()-_=+\|[]{};:',.&amp;lt;&amp;gt;?/ ", "ki")=' ' );
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 22:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-the-logic-for-a-WHERE-condition-in-a-PROC-SQL-using/m-p/508967#M136752</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-30T22:44:23Z</dc:date>
    </item>
  </channel>
</rss>

