<?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: Multiple variables with same value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417642#M102604</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data = have out=sorted;&lt;BR /&gt;by id year month;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(drop=i);&lt;BR /&gt;set sorted;&lt;BR /&gt;cnt=0;&lt;BR /&gt;array v(*) var1--var7;&lt;BR /&gt;do i = 1 to dim(v)-1;&lt;BR /&gt; if (v(i)=1 and v(i+1)=1) then cnt=cnt+1;&lt;BR /&gt;end;&lt;BR /&gt;if cnt&amp;gt;=1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=want out=final(where=(col1 ne 0));&lt;BR /&gt;by id year month;&lt;BR /&gt;var var1--var7;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select id, year, _name_,sum(col1) from final&lt;BR /&gt;group by id, year, _name_&lt;BR /&gt;having mean(month)=median(month) and sum(col1)&amp;gt;1;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2017 03:15:41 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2017-12-01T03:15:41Z</dc:date>
    <item>
      <title>Multiple variables with same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417606#M102589</link>
      <description>&lt;P&gt;&lt;SPAN class="token procnames"&gt;I want to keep only those id's having a flag=1 for two or more consecutive months for the same variables for the first as well as the second month. You see some id have a flag=1 for different variables in the first and second month.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I want only the same variables in the first month to contribute to the case not a different variable in the second month.&lt;/P&gt;
&lt;P&gt;Based on the data below, for instance id 1 have flag=1 for var 6 and 7 at month 7 and&amp;nbsp; 8.... this perfectly applies to my case.&lt;/P&gt;
&lt;P&gt;if you see id 2, there is a flag=1 for var 4 and 6 at month 12 2012, but in the next month, i.e. month 1 of 2013, the value for var 6 is 0 not 1 that leads id 2 to be excluded in the output.&lt;/P&gt;
&lt;P&gt;The output desired in the dataset have below is only id '1'.&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 have;
input id $ year month var1-var7;
datalines;
1 2012 7&amp;nbsp; 0 0 0 0 1 1 1
1 2012 8&amp;nbsp; 0 0 0 0 0 1 1
1 2012 9&amp;nbsp; 1 0 0 0 0 0 0
1 2012 10 0 0 1 0 0 0 0
1 2012 11 0 1 0 1 0 1 0
1 2012 12 0 0 0 0 0 0 1
1 2013 1&amp;nbsp; 0 0 1 1 1 0 0
2 2012 7&amp;nbsp; 0 0 0 0 0 0 0
2 2012 8&amp;nbsp; 0 0 0 0 0 0 0
2 2012 9&amp;nbsp; 0 0 0 0 0 0 0
2 2012 10 0 0 0 0 0 0 0
2 2012 11 0 0 0 0 0 0 0
2 2012 12 0 0 0 1 0 1 0
2 2013 1&amp;nbsp; 0 0 1 1 1 0 0
2 2013 3&amp;nbsp; 1 1 0 0 0 0 0
3 2012 7&amp;nbsp; 0 0 0 0 1 1 1
3 2012 8&amp;nbsp; 0 0 0 0 0 0 0
3 2012 9&amp;nbsp; 0 0 0 0 0 0 0
3 2012 10 0 0 1 0 0 0 0
4 2012 11 0 1 0 0 0 1 0
4 2012 12 0 0 0 0 0 0 0
4 2013 1&amp;nbsp; 1 0 0 0 0 1 0
4 2013 4&amp;nbsp; 0 0 1 1 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 00:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417606#M102589</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2017-12-01T00:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple variables with same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417633#M102601</link>
      <description>&lt;P&gt;ID1, included because of these records? Even though Var5 is not the same?&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 2012 7  0 0 0 0 1 1 1
1 2012 8  0 0 0 0 0 1 1&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID2, not included even though VAR4 is the same?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;2 2012 12 0 0 0 1 0 1 0
2 2013 1  0 0 1 1 1 0 0&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is inconsistent with the rule you've specified, so you either need to clarify your data or your rules.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to keep only those id's having a flag=1 for two or more consecutive months for the same variables for the first as well as the second month.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My translation of your criteria:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;Flag=1&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;On two or more consecutive months&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;On same variable&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&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/177968"&gt;@Abimal_Zippi&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN class="token procnames"&gt;I want to keep only those id's having a flag=1 for two or more consecutive months for the same variables for the first as well as the second month. You see some id have a flag=1 for different variables in the first and second month.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I want only the same variables in the first month to contribute to the case not a different variable in the second month.&lt;/P&gt;
&lt;P&gt;Based on the data below, for instance id 1 have flag=1 for var 6 and 7 at month 7 and&amp;nbsp; 8.... this perfectly applies to my case.&lt;/P&gt;
&lt;P&gt;if you see id 2, there is a flag=1 for var 4 and 6 at month 12 2012, but in the next month, i.e. month 1 of 2013, the value for var 6 is 0 not 1 that leads id 2 to be excluded in the output.&lt;/P&gt;
&lt;P&gt;The output desired in the dataset have below is only id '1'.&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 have;
input id $ year month var1-var7;
datalines;
1 2012 7&amp;nbsp; 0 0 0 0 1 1 1
1 2012 8&amp;nbsp; 0 0 0 0 0 1 1
1 2012 9&amp;nbsp; 1 0 0 0 0 0 0
1 2012 10 0 0 1 0 0 0 0
1 2012 11 0 1 0 1 0 1 0
1 2012 12 0 0 0 0 0 0 1
1 2013 1&amp;nbsp; 0 0 1 1 1 0 0
2 2012 7&amp;nbsp; 0 0 0 0 0 0 0
2 2012 8&amp;nbsp; 0 0 0 0 0 0 0
2 2012 9&amp;nbsp; 0 0 0 0 0 0 0
2 2012 10 0 0 0 0 0 0 0
2 2012 11 0 0 0 0 0 0 0
2 2012 12 0 0 0 1 0 1 0
2 2013 1&amp;nbsp; 0 0 1 1 1 0 0
2 2013 3&amp;nbsp; 1 1 0 0 0 0 0
3 2012 7&amp;nbsp; 0 0 0 0 1 1 1
3 2012 8&amp;nbsp; 0 0 0 0 0 0 0
3 2012 9&amp;nbsp; 0 0 0 0 0 0 0
3 2012 10 0 0 1 0 0 0 0
4 2012 11 0 1 0 0 0 1 0
4 2012 12 0 0 0 0 0 0 0
4 2013 1&amp;nbsp; 1 0 0 0 0 1 0
4 2013 4&amp;nbsp; 0 0 1 1 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 01:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417633#M102601</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-12-01T01:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple variables with same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417642#M102604</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data = have out=sorted;&lt;BR /&gt;by id year month;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(drop=i);&lt;BR /&gt;set sorted;&lt;BR /&gt;cnt=0;&lt;BR /&gt;array v(*) var1--var7;&lt;BR /&gt;do i = 1 to dim(v)-1;&lt;BR /&gt; if (v(i)=1 and v(i+1)=1) then cnt=cnt+1;&lt;BR /&gt;end;&lt;BR /&gt;if cnt&amp;gt;=1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=want out=final(where=(col1 ne 0));&lt;BR /&gt;by id year month;&lt;BR /&gt;var var1--var7;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select id, year, _name_,sum(col1) from final&lt;BR /&gt;group by id, year, _name_&lt;BR /&gt;having mean(month)=median(month) and sum(col1)&amp;gt;1;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 03:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417642#M102604</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2017-12-01T03:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple variables with same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417647#M102605</link>
      <description>&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The difference here, in id 1&amp;nbsp; there is a flag=1 for two or more variables where the names of the variables is&amp;nbsp;same for the two consecutive months&amp;nbsp;, but in id 2 there is a flag=1 for two or more variables but the name of the variables are different, so I will not count id2.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 04:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417647#M102605</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2017-12-01T04:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple variables with same value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417653#M102606</link>
      <description>&lt;P&gt;You've now clarified your object.&amp;nbsp; For any pair of consecutive observations within an ID, you want to assign flag=1 for&amp;nbsp;both of those observations if they have at least 2 variables with consecutive values of 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a single step solution that, for each ID, reads the series of observations twice.&amp;nbsp; In the first pass, each record F=1,2,3, ... is compared to the prior record and a temporary variable consec_ones{F} counts the number of vars with a value of 1 in the current and prior record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second pass, rereads the series, and for each record (S=1,2,3)&amp;nbsp;and if the&amp;nbsp;current record has consec_ones&amp;gt;1 or the next record has consec_ones &amp;gt; 1 then set the flag:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ year month var1-var7 ;
datalines;
1 2012 7  0 0 0 0 1 1 1
1 2012 8  0 0 0 0 0 1 1
1 2012 9  1 0 0 0 0 0 0
1 2012 10 0 0 1 0 0 0 0
1 2012 11 0 1 0 1 0 1 0
1 2012 12 0 0 0 0 0 0 1
1 2013 1  0 0 1 1 1 0 0
2 2012 6  0 0 1 1 1 0 0
2 2012 7  0 0 0 0 0 0 0
2 2012 8  0 0 0 0 0 0 0
2 2012 9  0 0 0 0 0 0 0
2 2012 10 0 0 0 0 0 0 0
2 2012 11 0 0 0 0 0 0 0
2 2012 12 0 0 0 1 0 1 0
2 2013 1  0 0 1 1 1 0 0
2 2013 3  1 1 0 0 0 0 0
3 2012 7  0 0 0 0 1 1 1
3 2012 8  0 0 0 0 0 0 0
3 2012 9  0 0 0 0 0 0 0
3 2012 10 0 0 1 0 0 0 0
4 2012 11 0 1 0 0 0 1 0
4 2012 12 0 0 0 0 0 0 0
4 2013 1  1 0 0 0 0 1 0
4 2013 4  0 0 1 1 0 0 0
run;


data want (drop=v f s);
  set have (in=firstpass)
      have (in=secondpass);
  by id;

  array consec_ones{30} _temporary_;
  array var{*} var: ;

  if first.id then call missing(F,S,of consec_ones{*});

  if firstpass then do;
    F+1;
    consec_ones{F}=0;
    do v=1 to dim(var);
      if dif(var{v})=0 and var{v}=1 then consec_ones{F}+1;
    end;
  end;
  if first.id then consec_ones{1}=0;

  if secondpass;
  S+1;
  flag= max(consec_ones{S},consec_ones{S+1})&amp;gt;1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&amp;nbsp; I added the &lt;EM&gt;&lt;STRONG&gt;if first.id then consec_ones{1}=0;&lt;/STRONG&gt;&lt;/EM&gt; statement to eliminate artificial creation of consec_one{1} from comparing the beginning of one ID to its predecessor at the end of the previous ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Set the size of the array consec_ones to at least 1 greater than the largest number of observations expected for any ID.&amp;nbsp; I used 30, meaning I expected no more that 29 records for any ID.&lt;/LI&gt;
&lt;LI&gt;F=1,2,3,... for record sequence within the first pass through an ID.&amp;nbsp;&amp;nbsp; S=1,2,3 for the same during the second pass.&lt;/LI&gt;
&lt;LI&gt;The &lt;EM&gt;&lt;STRONG&gt;DIF(x) function&lt;/STRONG&gt;&lt;/EM&gt; is equivalent to X-lag(X).&amp;nbsp; So if DIF(x)=0 and the current value of X=1, then you've revealed consecutive 1's.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The major "trick" here is to interleave dataset HAVE with itself&amp;nbsp; - the&amp;nbsp;pair of statements&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;SET HAVE (in=firstpass) HAVE (in=secondpass); BY ID;&lt;/STRONG&gt;&lt;/EM&gt; .&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 05:49:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-variables-with-same-value/m-p/417653#M102606</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-01T05:49:42Z</dc:date>
    </item>
  </channel>
</rss>

