<?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: How to check if condition using macros for a datset column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680629#M205737</link>
    <description>&lt;P&gt;Yes, the DISTINCT clause is a good point. However, you should trim the character values first, else you can get a very long macro string with a lot of blanks, and it is also a good idea to use single quotes rather than double quotes (see my answer).&lt;/P&gt;</description>
    <pubDate>Tue, 01 Sep 2020 06:51:51 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2020-09-01T06:51:51Z</dc:date>
    <item>
      <title>How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680618#M205726</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a condition to be added in my data step. it is as below :&lt;/P&gt;&lt;P&gt;IF BUSINESS_SEGMENT in &amp;lt;Business Segment from META_MVBS_VALID_VALUES&amp;gt; then do;&lt;BR /&gt;REL_YR_ID= 'OY';&lt;BR /&gt;REL_YR_VAL=strip(put(OCCYEAR, 4.));&lt;BR /&gt;end;&lt;BR /&gt;META_MVBS_VALID_VALUES is a table name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so i modify this as below :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select in_value into :bs_value separated by ',' from meta_mvbs_valid_values where column_name='BUSINESS_SEGMENT';&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And use this macro in in the if condition right? There i am stuck.&lt;/P&gt;&lt;P&gt;Can you pelase guide me how to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chithra&lt;/P&gt;</description>
      <pubDate>Tue, 01 Sep 2020 06:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680618#M205726</guid>
      <dc:creator>chithra</dc:creator>
      <dc:date>2020-09-01T06:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680623#M205731</link>
      <description>&lt;P&gt;Are your business segment values numeric or character? If your values are character then you need to have the values quoted in macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which might look like:&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
   select distinct quote(in_value) into :bs_value separated by ',' 
   from meta_mvbs_valid_values 
     where column_name='BUSINESS_SEGMENT';
quit;&lt;/PRE&gt;
&lt;P&gt;which would be used in a data step as&lt;/P&gt;
&lt;PRE&gt;IF BUSINESS_SEGMENT in (&amp;amp;bs_value.) then do;
   REL_YR_ID= 'OY';
   REL_YR_VAL=strip(put(OCCYEAR, 4.));
end;&lt;/PRE&gt;
&lt;P&gt;I used DISTINCT to reduce the number of potential elements in the list of values just in case there are any duplicates. The QUOTE function is so each of the values has quotes for use in the IN comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your values are numeric then you may have to provide examples because you should control the way the macro values are built by providing an appropriate format with PUT because the default conversion that goes on in the background might not do exactly as you want, especially if there are decimal values involved.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Sep 2020 06:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680623#M205731</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-01T06:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680625#M205733</link>
      <description>&lt;P&gt;This would only work for numeric values. If you work with character values, you need quotes around the individual values (use the QUOTE and STRIP function for removing leading and trailing blanks).&lt;/P&gt;
&lt;P&gt;This will only work until the maximum length of a macro variable (64k) is reached. A proper method that will scale much further is the use of a hash object, which also removes the need for an extra SQL step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_ = 1
then do;
  declare hash bs (dataset:"meta_mvbs_valid_values (
    rename=(in_value=business_segment)
    where=(column_name='BUSINESS_SEGMENT')
  )");
  bs.definekey("business_segment");
  bs.definedone();
end;
if bs.check() = 0
then do;
  REL_YR_ID = 'OY';
  REL_YR_VAL = strip(put(OCCYEAR, 4.));
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, since no usable (data step with datalines) example data was given.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Sep 2020 06:44:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680625#M205733</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-01T06:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680628#M205736</link>
      <description>&lt;P&gt;You just need to get the values into quotes, then you have it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select quote(trim(in_value),"'") into :bs_value separated by ',' 
from meta_mvbs_valid_values 
where column_name='BUSINESS_SEGMENT';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I used the QUOTE function rather than putting the quotation marks in directly, as this will take care of quotes in the actual values, and I used a single quote (the second parameter to the QUOTE function) rather than the standard double quote, in case your data contains stuff like "&amp;amp;" or "%", which the macro processor will try to interpret if values are in double quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you should be able to use your values in an IN clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF BUSINESS_SEGMENT in (&amp;amp;bs_value) then do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Sep 2020 06:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680628#M205736</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-09-01T06:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680629#M205737</link>
      <description>&lt;P&gt;Yes, the DISTINCT clause is a good point. However, you should trim the character values first, else you can get a very long macro string with a lot of blanks, and it is also a good idea to use single quotes rather than double quotes (see my answer).&lt;/P&gt;</description>
      <pubDate>Tue, 01 Sep 2020 06:51:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680629#M205737</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-09-01T06:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if condition using macros for a datset column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680661#M205754</link>
      <description>&lt;P&gt;this worked.&lt;/P&gt;&lt;P&gt;Many Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Sep 2020 11:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-condition-using-macros-for-a-datset-column/m-p/680661#M205754</guid>
      <dc:creator>chithra</dc:creator>
      <dc:date>2020-09-01T11:11:03Z</dc:date>
    </item>
  </channel>
</rss>

