<?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: sas macro - subset dataset if variable contains string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787130#M251434</link>
    <description>&lt;OL&gt;
&lt;LI&gt;Refer to macro variables using &amp;amp;&lt;/LI&gt;
&lt;LI&gt;CONTAINS is not a valid SAS function (SQL function)&lt;/LI&gt;
&lt;LI&gt;Because you want to introduce conditional filters, you need to use macro %IF/%THEN logic, not data step IF/THEN logic.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create dataset*/
%macro prob(var1=, var2=);
data demand&amp;amp;var1.problem;
	set qul.test0;
/*Need to use two seperate where condition to subset data based on var2 value*/
	%if %sysfunc(find(&amp;amp;var2.,  "try1")&amp;gt;0) %then %do;
		where &amp;amp;var2. ne 0;
	%else
		where &amp;amp;var2. &amp;gt; 0;
run;
%mend;
%prob(var1=Fcst, var2=test_try1);
%prob(var1=sd, var2=test_test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However your logic doesn't make sense to me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're testing if VAR2 contains the value of TRY1, which implies a character variable.&lt;/P&gt;
&lt;P&gt;But then adding a WHERE clause on VAR2 being greater than 0 or equal to 0, which implies a numeric variable.&amp;nbsp;&lt;BR /&gt;So this is a bit inconsistent.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should one of those be var1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do need to subset my dataset by two different condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting LINE and COLUMN cannot be determined error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;/*Create dataset*/
%macro prob(var1=, var2=);
data demand&amp;amp;var1.problem;
	set qul.test0;
/*Need to use two seperate where condition to subset data based on var2 value*/
	if var2. contains "try1" then 
		where var2. ne 0;
	else 
		where var2. &amp;gt; 0;
run;
%mend;
%prob(var1=Fcst, var2=test_try1);
%prob(var1=sd, var2=test_test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Dec 2021 16:18:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-12-22T16:18:31Z</dc:date>
    <item>
      <title>sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787112#M251424</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do need to subset my dataset by two different condition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting LINE and COLUMN cannot be determined error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*Create dataset*/
%macro prob(var1=, var2=);
data demand&amp;amp;var1.problem;
	set qul.test0;
/*Need to use two seperate where condition to subset data based on var2 value*/
	if var2. contains "try1" then 
		where var2. ne 0;
	else 
		where var2. &amp;gt; 0;
run;
%mend;
%prob(var1=Fcst, var2=test_try1);
%prob(var1=sd, var2=test_test);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 14:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787112#M251424</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-22T14:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787113#M251425</link>
      <description>&lt;P&gt;you have to refer to all macro variables with an ampersand&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	if &amp;amp;var2. contains "try1" then &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787113#M251425</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-22T15:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787115#M251426</link>
      <description>&lt;P&gt;Still I am having an error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using following logic:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;if &amp;amp;var2. contains "try1" then 
where var2. ne 0;
else 
where var2. &amp;gt; 0;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787115#M251426</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-22T15:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787117#M251428</link>
      <description>&lt;P&gt;Please correct your code using what I said a minute ago (with emphasis)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;you have to refer to &lt;FONT color="#FF0000"&gt;ALL&lt;/FONT&gt; macro variables with an ampersand"&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787117#M251428</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-22T15:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787119#M251429</link>
      <description>&lt;P&gt;Still no luck&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787119#M251429</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-12-22T15:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787121#M251430</link>
      <description>&lt;P&gt;What are you trying to test with your IF statement?&amp;nbsp; Are you trying to test if the macro parameter VAR2 contains the string TRY1 ?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you need to use&lt;STRONG&gt; macro logic&lt;/STRONG&gt; to conditionally generate different WHERE statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %sysfunc(find(&amp;amp;var2,try1,i)) %then %do;
  where &amp;amp;var2. ne 0;
%end;
%else %do;
  where &amp;amp;var2. &amp;gt; 0;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If instead you need to check the values of actual data then you need to do something else.&amp;nbsp;You cannot conditionally execute a WHERE statement.&amp;nbsp; The WHERE statement operates during the loading of the data, so before any IF statement could execute.&amp;nbsp; If you need to decide which observations to continue using based on the values of the actual data then use either a subsetting IF or a DELETE statement.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:19:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787121#M251430</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-22T15:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787122#M251431</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Still no luck&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;and I can't help you further, as you didn't say what you did or what errors you have. So I have no idea what the problem is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you provide that information, then I can help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, for your future benefit (and also in this case), when you have code that doesn't work, you must show us the log (not the actual code) so we can see what you did and what the errors were. We need to see the ENTIRE log, every single line, every single character, with nothing removed. You need to do this from now on whenever your code isn't working, no exceptions, and don't wait until we ask to see the log.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:20:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787122#M251431</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-22T15:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787123#M251432</link>
      <description>&lt;P&gt;Step one of all macro programming: start with working non-macro code.&lt;/P&gt;
&lt;P&gt;So please show us the non-macro code you start with.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787123#M251432</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-22T15:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787124#M251433</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;is right (as always), and I am going to have to insist that you start by showing us working valid SAS code with no macros and no macro variables. Why? Because you don't have working SAS code, and so your macro will never work either.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 15:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787124#M251433</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-22T15:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: sas macro - subset dataset if variable contains string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787130#M251434</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Refer to macro variables using &amp;amp;&lt;/LI&gt;
&lt;LI&gt;CONTAINS is not a valid SAS function (SQL function)&lt;/LI&gt;
&lt;LI&gt;Because you want to introduce conditional filters, you need to use macro %IF/%THEN logic, not data step IF/THEN logic.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create dataset*/
%macro prob(var1=, var2=);
data demand&amp;amp;var1.problem;
	set qul.test0;
/*Need to use two seperate where condition to subset data based on var2 value*/
	%if %sysfunc(find(&amp;amp;var2.,  "try1")&amp;gt;0) %then %do;
		where &amp;amp;var2. ne 0;
	%else
		where &amp;amp;var2. &amp;gt; 0;
run;
%mend;
%prob(var1=Fcst, var2=test_try1);
%prob(var1=sd, var2=test_test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However your logic doesn't make sense to me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're testing if VAR2 contains the value of TRY1, which implies a character variable.&lt;/P&gt;
&lt;P&gt;But then adding a WHERE clause on VAR2 being greater than 0 or equal to 0, which implies a numeric variable.&amp;nbsp;&lt;BR /&gt;So this is a bit inconsistent.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should one of those be var1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do need to subset my dataset by two different condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting LINE and COLUMN cannot be determined error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;/*Create dataset*/
%macro prob(var1=, var2=);
data demand&amp;amp;var1.problem;
	set qul.test0;
/*Need to use two seperate where condition to subset data based on var2 value*/
	if var2. contains "try1" then 
		where var2. ne 0;
	else 
		where var2. &amp;gt; 0;
run;
%mend;
%prob(var1=Fcst, var2=test_try1);
%prob(var1=sd, var2=test_test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 16:18:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-macro-subset-dataset-if-variable-contains-string/m-p/787130#M251434</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-22T16:18:31Z</dc:date>
    </item>
  </channel>
</rss>

