<?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: IF column exist then apply where condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867946#M342803</link>
    <description>&lt;P&gt;Start a new, pristine SAS session.&lt;/P&gt;
&lt;P&gt;Then run this (only this) and read the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X y z W $;
cards;
10 20 30 A
40 25 38 B
20 40 80 C
;
Run;

/****Case1****/
/****Case1****/
/****Case1****/
%let User_Col_name = Q;
%let Value_Select =10;

proc sql noprint;
select count(*) into :IndExist_User_Col
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and upcase(name) = upcase("&amp;amp;User_Col_name.");
quit;
%put &amp;amp;IndExist_User_Col;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Apr 2023 09:39:19 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-04-04T09:39:19Z</dc:date>
    <item>
      <title>IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867931#M342796</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the following code user define a column name and value to select rows.&lt;/P&gt;
&lt;P&gt;If the user define a column that doesn't exist in data set then the want data set should contain all rows.&lt;/P&gt;
&lt;P&gt;If the user define a column that&amp;nbsp; exist in data set then the want data set should contain rows after using where condition.&lt;/P&gt;
&lt;P&gt;In "case1" of my code I expect to get all rows and I get 0 rows in want data set- why??&lt;/P&gt;
&lt;P&gt;May you help to correct the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X y z W $;
cards;
10 20 30 A
40 25 38 B
20 40 80 C
;
Run;

/****Case1****/
/****Case1****/
/****Case1****/
%let User_Col_name = Q;
%let Value_Select =10;


proc sql noprint;
select count(*) into :IndExist_User_Col
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and upcase(name) = upcase("&amp;amp;User_Col_name.");
quit;
%put &amp;amp;IndExist_User_Col;

data want;
set have;
if &amp;amp;IndExist_User_Col=1 then do;
IF &amp;amp;User_Col_name.=&amp;amp;Value_Select. then output;
end;
run;
/**Problem!!!I expect to see all rows***/

/****Case2****/
/****Case2****/
/****Case2****/
%let User_Col_name = X;
%let Value_Select =10;


proc sql noprint;
select count(*) into :IndExist_User_Col
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and upcase(name) = upcase("&amp;amp;User_Col_name.");
quit;
%put &amp;amp;IndExist_User_Col;

data want;
set have;
if &amp;amp;IndExist_User_Col=1 then do;
IF &amp;amp;User_Col_name.=&amp;amp;Value_Select. then output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 08:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867931#M342796</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-04-04T08:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867933#M342797</link>
      <description>&lt;LI-CODE lang="sas"&gt;memname = "HAVE" ?&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 08:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867933#M342797</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-04T08:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867935#M342799</link>
      <description>&lt;P&gt;and:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;IndExist_User_Col=1 then do;
IF &amp;amp;User_Col_name.=&amp;amp;Value_Select. then output;
end;
&lt;STRONG&gt;else output;&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 08:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867935#M342799</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-04T08:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867938#M342800</link>
      <description>&lt;P&gt;you can also consider macro %if-statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
  %if &amp;amp;IndExist_User_Col=1 %then %do;
    where &amp;amp;User_Col_name.=&amp;amp;Value_Select.;
  %end;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 08:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867938#M342800</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-04T08:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867941#M342801</link>
      <description>&lt;P&gt;Yes, sorry ,by mistake it was written ttt but I fixed it&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 08:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867941#M342801</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-04-04T08:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867945#M342802</link>
      <description>&lt;P&gt;I played a bit with datastep searching &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options fullstimer;
%macro FilterData(
 have = SASHELP.CLASS
,want = WORK.WANT
,User_Col_name = AGE
,Value_Select = 14
);

%local IndExist_User_Col; %let IndExist_User_Col=0;
data _null_;
  if 0 then set &amp;amp;have.;
  length __name $ 32 __type $ 1 __length 8;
  do _N_ = 1 by 1 until(^__length or __name='__name');
    call vnext(__name, __type, __length);
    /*put _all_;*/
    if upcase(__name) = %upcase("&amp;amp;User_Col_name.") and __type = "N" then
      do;
        call symputx("IndExist_User_Col", 1, "L");
        stop;
      end;     
  end;
  stop;
run;
%put &amp;amp;=IndExist_User_Col;

data &amp;amp;want.;
  set &amp;amp;have.;
  %if &amp;amp;IndExist_User_Col=1 %then %do;
    where &amp;amp;User_Col_name.=&amp;amp;Value_Select.;
  %end;
  output;
run;
%mend FilterData;

%FilterData();

%FilterData(User_Col_name = NAME);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;[EDIT] I added `%local IndExist_User_Col;` to macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 09:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867945#M342802</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-04T09:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867946#M342803</link>
      <description>&lt;P&gt;Start a new, pristine SAS session.&lt;/P&gt;
&lt;P&gt;Then run this (only this) and read the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X y z W $;
cards;
10 20 30 A
40 25 38 B
20 40 80 C
;
Run;

/****Case1****/
/****Case1****/
/****Case1****/
%let User_Col_name = Q;
%let Value_Select =10;

proc sql noprint;
select count(*) into :IndExist_User_Col
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and upcase(name) = upcase("&amp;amp;User_Col_name.");
quit;
%put &amp;amp;IndExist_User_Col;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 09:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/867946#M342803</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-04T09:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868089#M342875</link>
      <description>Thanks, &lt;BR /&gt;Why is it needed to add the sentence of else ouput?</description>
      <pubDate>Tue, 04 Apr 2023 21:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868089#M342875</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-04-04T21:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868090#M342876</link>
      <description>Thanks&lt;BR /&gt;May you please explain why my code didnt work?&lt;BR /&gt;if &amp;amp;IndExist_User_Col=1 then do;&lt;BR /&gt;IF &amp;amp;User_Col_name.=&amp;amp;Value_Select. then output;&lt;BR /&gt;end;</description>
      <pubDate>Tue, 04 Apr 2023 21:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868090#M342876</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-04-04T21:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868093#M342879</link>
      <description>What do you mean "pristine sas session".&lt;BR /&gt;This cose work 100% but it is only part of the code i run</description>
      <pubDate>Tue, 04 Apr 2023 21:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868093#M342879</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-04-04T21:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868111#M342894</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks&lt;BR /&gt;May you please explain why my code didnt work?&lt;BR /&gt;if &amp;amp;IndExist_User_Col=1 then do;&lt;BR /&gt;IF &amp;amp;User_Col_name.=&amp;amp;Value_Select. then output;&lt;BR /&gt;end;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Adding the ELSE will cause it to output ALL of the observations when the first condition is FALSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run a normal data step that does not include any OUTPUT statements then SAS will add "implied output" at the end of the step.&amp;nbsp; But once you have any OUTPUT statements it will not do the implied output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your code works fine when the first IF condition is TRUE.&amp;nbsp; It then runs the second if and conditionally writes the observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But when the first IF is FALSE then it never executes any OUTPUT statements.&amp;nbsp; So you always get zero observations written.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also redo the logic to avoid the use of the OUTPUT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;IndExist_User_Col=1 then do;
  IF &amp;amp;User_Col_name. NE &amp;amp;Value_Select. then DELETE;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 23:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868111#M342894</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-04T23:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: IF column exist then apply where condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868127#M342902</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;What do you mean "pristine sas session".&lt;BR /&gt;This cose work 100% but it is only part of the code i run&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Pristine = unsullied, unmodified&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which means (for a SAS session) no previous code of yours has already set macro variables or altered anything from the startup state.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first SQL will not find any observations because of the WHERE clause, so the macro variable is not created.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 04:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-column-exist-then-apply-where-condition/m-p/868127#M342902</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-05T04:46:54Z</dc:date>
    </item>
  </channel>
</rss>

