<?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: New column with Ind of max in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770490#M244460</link>
    <description>&lt;P&gt;Hi Peter.C ,&lt;/P&gt;
&lt;P&gt;If data look like this, your code would get wrong result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data have;&lt;BR /&gt;input CustID Model $ Y;&lt;BR /&gt;cards;&lt;BR /&gt;1 A 100&lt;BR /&gt;1 B 80&lt;BR /&gt;2 A 70&lt;BR /&gt;2 B 90&lt;BR /&gt;&lt;STRONG&gt;3 B 60&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;3 A 60&lt;/STRONG&gt;&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;</description>
    <pubDate>Sun, 26 Sep 2021 12:42:58 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-09-26T12:42:58Z</dc:date>
    <item>
      <title>New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770477#M244451</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Each customer&amp;nbsp; have 2 rows with&amp;nbsp; information of two models.&lt;/P&gt;
&lt;P&gt;I want to create a new columns called Ind with the following results:&lt;/P&gt;
&lt;P&gt;If the model that produce the max is "A" then&amp;nbsp; in row of "A" will have value 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the model that produce the max is "B" then&amp;nbsp; in row of "B" will have value 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the model that produce the max is both "A" and&amp;nbsp; "B" then&amp;nbsp; in row of "A" will have value 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&lt;/P&gt;
&lt;P&gt;Wanted table is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ronein_0-1632652510979.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64074i7BE02459AE673C3E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ronein_0-1632652510979.png" alt="Ronein_0-1632652510979.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Sep 2021 10:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770477#M244451</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-09-26T10:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770478#M244452</link>
      <description>&lt;P&gt;This identifies which model has the maximum value of Y. I assume you can create the desired values of IND from this information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class custid;
    var y;
    output out=data_max max=max_y idgroup(max(y) out[1](model)=model_max_y);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Sep 2021 12:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770478#M244452</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-26T12:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770487#M244457</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
;
Run;

data want(drop = mY n);
   do _N_ = 1 by 1 until(last.CustID);
      set have;
      by CustID;
      if Y &amp;gt; mY then do;
         mY = Y;
         n = _N_;
      end;
   end;
   
   do _N_ = 1 to _N_;
      set have;
      Ind = _N_ = n;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;CustID Model Y   Ind
1      A     100 1
1      B     80  0
2      A     70  0
2      B     90  1
3      A     60  1
3      B     60  0&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Sep 2021 12:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770487#M244457</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-26T12:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770489#M244459</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
;
Run;

proc sql;
create table want as
select *,
case 
when range(Y)=0 then 
  case 
  when Model='A' then 1 
  else 0
  end
when Y=max(Y) then 1 else 0 
end as Ind
 from have
  group by CustID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Sep 2021 12:38:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770489#M244459</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-26T12:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770490#M244460</link>
      <description>&lt;P&gt;Hi Peter.C ,&lt;/P&gt;
&lt;P&gt;If data look like this, your code would get wrong result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data have;&lt;BR /&gt;input CustID Model $ Y;&lt;BR /&gt;cards;&lt;BR /&gt;1 A 100&lt;BR /&gt;1 B 80&lt;BR /&gt;2 A 70&lt;BR /&gt;2 B 90&lt;BR /&gt;&lt;STRONG&gt;3 B 60&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;3 A 60&lt;/STRONG&gt;&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Sep 2021 12:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770490#M244460</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-26T12:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770496#M244461</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table max as select custID, max(Y) as max
		from have
		group by custid
		having Y=max(Y);
		
	create table want as select distinct h.custid, h.model, h.y, m.max
		from have as h
		left join
		max as m
		on m.custid=h.custid;
quit;

data want2;
	set want;
	if y ne lag(y) then do;
	if y=max then ind=1;
	else ind=0;
		end;
	if y=lag(y) then do;
	if y=max and model='A' then ind=1;
	else ind=0;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Sep 2021 13:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770496#M244461</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-26T13:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770541#M244473</link>
      <description>&lt;P&gt;You can merge each observation with the next observation.&amp;nbsp; &amp;nbsp;So when the current observation is an "A", you can compare y to the next y, otherwise compare y to the previous y.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program requires there to be exactly 2 models per cust_id, with model A preceding the model B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
Run;

data want (drop=_:);
  merge have
        have (firstobs=2 keep=y rename=(y=_next_y));
  ind=ifn(model='A',(y&amp;gt;=_next_y),(y&amp;gt;lag(y)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note this IFN function produces the correct results, unlike the &lt;EM&gt;&lt;STRONG&gt;apparently&lt;/STRONG&gt;&lt;/EM&gt; equivalent two statements below, which will produce incorrect results for custid4 below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
4 A 70
4 B 65
;
Run;

data want (drop=_:);
  merge have
        have (firstobs=2 keep=y rename=(y=_next_y));
  if model='A' then ind=(y&amp;gt;=_next_y);
  else ind=(y&amp;gt;lag(y));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is because the LAG function is not like a one-row lookback in Excel.&amp;nbsp; Instead it is a queue-update.&amp;nbsp; And if the queue is updated only when the model ^='A', then each y for a B is compared to the prior B, not the preceding A.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the IFN function will run the LAG for every observation regardless of the model value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Sep 2021 20:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770541#M244473</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-09-26T20:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770571#M244489</link>
      <description>&lt;P&gt;Shoot, you're right. Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Sep 2021 05:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770571#M244489</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-27T05:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770645#M244503</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input CustID Model $ Y;
cards;
1 A 100
1 B 80
2 A 70
2 B 90
3 A 60
3 B 60
;
RUN;

PROC SORT;BY custID descending Y Model;RUN;

DATA want;
   length ind 8;
   set have;
   BY custID descending Y Model;
   ind=0;
   if first.custID then ind=1;
RUN;

PROC SORT;BY custID Model;RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Sep 2021 14:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/770645#M244503</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2021-09-27T14:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: New column with Ind of max</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/771066#M244629</link>
      <description>I think it is not working well,&lt;BR /&gt;look at ID=1 , the ind variable get value 1 for both rows and need only in one row</description>
      <pubDate>Wed, 29 Sep 2021 05:20:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-column-with-Ind-of-max/m-p/771066#M244629</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-09-29T05:20:13Z</dc:date>
    </item>
  </channel>
</rss>

