<?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: Check If a Value Queried Contains a Value in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189040#M4412</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To answer your original question first, here's the way I prefer to see whether a macro variable has a non-null value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;max_level_total) %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An entire paper has been written comparing the methods, so this is just my preference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just as important, you may need to understand the behavior that you are noticing.&amp;nbsp; When SQL extracts data into a macro variable, it leaves the macro variable unchanged when 0 rows are selected.&amp;nbsp; That's different than when SQL extracts a statistic (such as SUM) into a macro variable.&amp;nbsp; Also note that SQL does not reset macro variables when extracting 0 rows.&amp;nbsp; So if you run the same code twice in the same program, and only the first instance returns rows, the macro variable remains the same after the second instance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 02 Mar 2015 21:33:42 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2015-03-02T21:33:42Z</dc:date>
    <item>
      <title>Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189036#M4408</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="background: white;"&gt;&lt;SPAN style="font-family: Arial, sans-serif; background-position: initial;"&gt;Is there a way in SAS 9.2 to write a check to see if a value queried contains a value?&amp;nbsp; In this particular case I am dealing with numerical values. E.g.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="background: white;"&gt;&lt;SPAN style="font-family: Arial, sans-serif; background-position: initial;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt;SELECT total into :max_level_total&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt;From enrollment;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt;The step after this I tried dividing my numerical blank value by another numerical blank value and got an error.&amp;nbsp; This occurred when my query returned 0 rows.&amp;nbsp; I would like to include an if statement to handle this.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Arial',sans-serif; background: white;"&gt;A flag variable for if the query returned rows or not would also solve what I need.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Mar 2015 20:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189036#M4408</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2015-03-02T20:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189037#M4409</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use the automatic macro variable SQLOBS?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/viewer.htm#a001360983.htm" title="http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/viewer.htm#a001360983.htm"&gt;SAS(R) 9.2 SQL Procedure User's Guide&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SQLOBS&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="margin: 0 0 1.4em;"&gt;contains the number of rows that were processed by an SQL procedure statement. For example, the SQLOBS macro variable contains the number of rows that were formatted and displayed in SAS output by a SELECT statement or the number of rows that were deleted by a DELETE statement.&lt;/P&gt;&lt;P style="margin: 0 0 1.4em;"&gt;When the NOPRINT option is specified, the value of the SQLOBS macro variable depends on whether an output table, single macro variable, macro variable list, or macro variable range is created:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN style="margin: 1.4em 0 0;"&gt;If no output table, macro variable list, or macro variable range is created, then SQLOBS contains the value 1.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="margin: 1.4em 0 0;"&gt;If an output table is created, then SQLOBS contains the number of rows in the output table.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="margin: 1.4em 0 0;"&gt;If a single macro variable is created, then SQLOBS contains the value 1.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="margin: 1.4em 0 0;"&gt;If a macro variable list or macro variable range is created, then SQLOBS contains the number of rows that are processed to create the macro variable list or range.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P style="margin: 0 0 1.4em;"&gt;If an SQL view is created, then SQLOBS contains the value 0.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Mar 2015 20:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189037#M4409</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-03-02T20:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189038#M4410</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Perhaps this may help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*******************/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*** sample data ***/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*******************/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data t_a;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;input a1 @@;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;cards;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;0 1 2 2 7 8 9 6&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*********************************************/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*** a PROC SQL query which deals with the ***/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*** possibility that the denominator = 0&amp;nbsp; ***/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;/*********************************************/&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; select&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (case when sum(10&amp;lt;=a1&amp;lt;=12) = 0 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; then 999.99 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else round(sum(2&amp;lt;=a1&amp;lt;=5)/sum(10&amp;lt;=a1&amp;lt;=12),01) end),&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (case when sum(10&amp;lt;=a1&amp;lt;=12) = 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; then 'Y' else 'N' end)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; into :m_test, :zero_denom&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; from t_a;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;%put testing macro variable m_test &amp;amp;m_test;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;%put testing macro variable zero_denom &amp;amp;zero_denom;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;produces in the log file:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;testing macro variable m_test&amp;nbsp;&amp;nbsp; 999.99&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;testing macro variable zero_denom Y&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Mar 2015 21:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189038#M4410</guid>
      <dc:creator>billfish</dc:creator>
      <dc:date>2015-03-02T21:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189039#M4411</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could initialize the macro variable with a '.' (a missing). If 0 rows returned then it will remain missing else it will get the value from your query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let max_level_total=.;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp; SELECT age into :max_level_total&lt;/P&gt;&lt;P&gt;&amp;nbsp; From sashelp.class where name='xxx'&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put max_level_total: &amp;amp;max_level_total;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Not sure if the code you've posted is representative for your real code. In your example your query could return multiple rows. Only the last value would remain in the macro variable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Mar 2015 21:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189039#M4411</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-03-02T21:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189040#M4412</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To answer your original question first, here's the way I prefer to see whether a macro variable has a non-null value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;max_level_total) %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An entire paper has been written comparing the methods, so this is just my preference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just as important, you may need to understand the behavior that you are noticing.&amp;nbsp; When SQL extracts data into a macro variable, it leaves the macro variable unchanged when 0 rows are selected.&amp;nbsp; That's different than when SQL extracts a statistic (such as SUM) into a macro variable.&amp;nbsp; Also note that SQL does not reset macro variables when extracting 0 rows.&amp;nbsp; So if you run the same code twice in the same program, and only the first instance returns rows, the macro variable remains the same after the second instance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Mar 2015 21:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189040#M4412</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-03-02T21:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Check If a Value Queried Contains a Value</title>
      <link>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189041#M4413</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="background: white; font-family: 'Arial',sans-serif; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;"&gt;The built in SQL count methods are efficient. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; font-family: 'Arial',sans-serif; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;"&gt;I was running similar code twice where I had an insert into in one block and called the same insert into&lt;BR /&gt;in a later block which would run into the scenario that Astounding described.&amp;nbsp; It may be a good practice to use a different&lt;BR /&gt;variable name in the second insert into block.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Mar 2015 13:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Check-If-a-Value-Queried-Contains-a-Value/m-p/189041#M4413</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2015-03-03T13:55:09Z</dc:date>
    </item>
  </channel>
</rss>

