<?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: where statement:  multi variables have the same value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429843#M106207</link>
    <description>&lt;P&gt;Unfortunately you can't use variable lists inside a WHERE statement, but you can with an IF statement. So it depends on whether you're looking for efficiency in terms of your time or processing time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick is to add the variables together, and if they add to the expected number then you can easily filter the observations needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If sum(of flag1-flag5) = 5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know how many variables you have&amp;nbsp;or need to account for missing values the following is also an option:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sum(of flag1-flag5) = n(of flag1-flag5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I have a question about how to use the WHERE statement in data step and in proc sql. My data is like below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old;
input flag1 flag2 flag3 flag4 flag5;
datalines;
0 1 0 1 0
1 1 1 1 1
1 1 1 1 0
0 0 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to keep the subset that all flag variables have value of 1. In this case, the result should only contain the second row. I do have more flag variables than just 5. I don't want to write "where flag1=1 and flag2=1 and ....". How should I do that? Just want to fill the where statement below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new as
select *,
from old
where ??????;
quit;

data new;
  set old;
  where ??????????;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 23 Jan 2018 00:57:35 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-01-23T00:57:35Z</dc:date>
    <item>
      <title>where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429839#M106205</link>
      <description>&lt;P&gt;I have a question about how to use the WHERE statement in data step and in proc sql. My data is like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old;
input flag1 flag2 flag3 flag4 flag5;
datalines;
0 1 0 1 0
1 1 1 1 1
1 1 1 1 0
0 0 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to keep the subset that all flag variables have value of 1. In this case, the result should only contain the second row. I do have more flag variables than just 5. I don't want to write "where flag1=1 and flag2=1 and ....". How should I do that? Just want to fill the where statement below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new as
select *,
from old
where ??????;
quit;

data new;
  set old;
  where ??????????;
  run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 00:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429839#M106205</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2018-01-23T00:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429843#M106207</link>
      <description>&lt;P&gt;Unfortunately you can't use variable lists inside a WHERE statement, but you can with an IF statement. So it depends on whether you're looking for efficiency in terms of your time or processing time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick is to add the variables together, and if they add to the expected number then you can easily filter the observations needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If sum(of flag1-flag5) = 5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know how many variables you have&amp;nbsp;or need to account for missing values the following is also an option:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sum(of flag1-flag5) = n(of flag1-flag5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I have a question about how to use the WHERE statement in data step and in proc sql. My data is like below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old;
input flag1 flag2 flag3 flag4 flag5;
datalines;
0 1 0 1 0
1 1 1 1 1
1 1 1 1 0
0 0 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to keep the subset that all flag variables have value of 1. In this case, the result should only contain the second row. I do have more flag variables than just 5. I don't want to write "where flag1=1 and flag2=1 and ....". How should I do that? Just want to fill the where statement below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new as
select *,
from old
where ??????;
quit;

data new;
  set old;
  where ??????????;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 00:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429843#M106207</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-23T00:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429847#M106210</link>
      <description>Thanks for your reply! The trick works for my test sample. But my exact data will have millions of observations and more than 30 flag variables. Does the IF trick will increase a lot of my processing time? I am not sure.</description>
      <pubDate>Tue, 23 Jan 2018 01:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429847#M106210</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2018-01-23T01:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429849#M106211</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data old;
input flag1 flag2 flag3 flag4 flag5;
datalines;
0 1 0 1 0
1 1 1 1 1
1 1 1 1 0
0 0 0 0 0
;

data want;
set old;
array t(*) flag:;
if whichn(0,of t(*)) eq 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jan 2018 01:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429849#M106211</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-23T01:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429852#M106212</link>
      <description>&lt;P&gt;Thanks, the code also works well. Just wondering can it be used in proc sql?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 01:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429852#M106212</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2018-01-23T01:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429857#M106215</link>
      <description>&lt;P&gt;Variable lists and/or arrays can't be used in PROC SQL but WHICHN can be used.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable list is relatively short (30 is fine) then you can also create a macro variable with the list of variables from the SASHELP VCOLUMN table and use that in your WHERE clause instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll have to adjust the first portion of the query to match your variable list notation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

select name into :var_list separated by ', '
from sashelp.vcolumn
where libname='WORK' and %upcase(memname)='MYDATA' and name like 'MYVAR%';
quit;

proc sql;
create table want as
select *
from WORK.MYDATA
where whichn(0, &amp;amp;var_list) = 0;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jan 2018 02:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/429857#M106215</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-23T02:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: where statement:  multi variables have the same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/430063#M106281</link>
      <description>&lt;P&gt;Thank you! That's good to know.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 16:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-statement-multi-variables-have-the-same-value/m-p/430063#M106281</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2018-01-23T16:14:11Z</dc:date>
    </item>
  </channel>
</rss>

