<?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 Value in last row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516242#M139402</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer there are multiple rows.&lt;/P&gt;
&lt;P&gt;I want to add a new field (binary variable that get value 1 or 0)&lt;/P&gt;
&lt;P&gt;IF the value of X in &amp;nbsp;last row of customer &amp;nbsp;is equal or greater than 1 &amp;nbsp;then newField=1;&lt;/P&gt;
&lt;P&gt;else&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;newField=0;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Expected values&amp;nbsp;in Newfield for customer 1 are:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Expected values&amp;nbsp;in Newfield&amp;nbsp;for customer 2&amp;nbsp;are:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why the code below is not giving the wanted result?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
input ID x;
cards;
1 0
1 0
1 1
1 2
1 3
2 0 
2 0 
2 0 
2 0 
2 0 
;
run;


data c2;
set c1;
IF last.ID and x&amp;gt;=1 then Newfield=1;else Newfield=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Nov 2018 08:20:24 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2018-11-27T08:20:24Z</dc:date>
    <item>
      <title>Value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516242#M139402</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer there are multiple rows.&lt;/P&gt;
&lt;P&gt;I want to add a new field (binary variable that get value 1 or 0)&lt;/P&gt;
&lt;P&gt;IF the value of X in &amp;nbsp;last row of customer &amp;nbsp;is equal or greater than 1 &amp;nbsp;then newField=1;&lt;/P&gt;
&lt;P&gt;else&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;newField=0;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Expected values&amp;nbsp;in Newfield for customer 1 are:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Expected values&amp;nbsp;in Newfield&amp;nbsp;for customer 2&amp;nbsp;are:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why the code below is not giving the wanted result?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
input ID x;
cards;
1 0
1 0
1 1
1 2
1 3
2 0 
2 0 
2 0 
2 0 
2 0 
;
run;


data c2;
set c1;
IF last.ID and x&amp;gt;=1 then Newfield=1;else Newfield=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Nov 2018 08:20:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516242#M139402</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-11-27T08:20:24Z</dc:date>
    </item>
    <item>
      <title>Re: Value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516247#M139407</link>
      <description>&lt;P&gt;What is the sorting variables for that data.&amp;nbsp; The simplest method to get what you want is to reverse sort the data - so the last record per id is first, then retain that down the data, e.g.:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain x;
  if first.id then new_x=ifn(x&amp;gt;1,1,0);
run;&lt;/PRE&gt;
&lt;P&gt;Then sort back to what it is.&lt;/P&gt;
&lt;P&gt;If you can't sort your data - and if you cant then your data is not good for working with - then you will need to merge the result back on, something like:&lt;/P&gt;
&lt;PRE&gt;data inter;
  set have;
  by id;
  if last.id then output;
run;

data want (drop=base);
  merge have inter (rename=(x=base));
  by id;
  x=ifn(x&amp;gt;1,1,0);
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Nov 2018 09:01:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516247#M139407</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-27T09:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: Value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516289#M139432</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
input ID x;
cards;
1 0
1 0
1 1
1 2
1 3
2 0 
2 0 
2 0 
2 0 
2 0 
;
run;

data want;

do until(last.id);
 set c1;
 by id;
end;
if x&amp;gt;=1 then Newfield=1;else Newfield=0;
do until(last.id);
 set c1;
 by id;
 output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Nov 2018 12:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516289#M139432</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-11-27T12:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516314#M139442</link>
      <description>&lt;P&gt;Your code is very close to working.&amp;nbsp; It is missing the BY statement in the DATA step.&amp;nbsp; The BY statement is what creates first.ID and last.ID:&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 procnames"&gt;data&lt;/SPAN&gt; c2&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; c1&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;FONT color="#FF0000"&gt;by id;&lt;/FONT&gt;
&lt;SPAN class="token keyword"&gt;IF&lt;/SPAN&gt; last&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt; and x&lt;SPAN class="token operator"&gt;&amp;gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; Newfield&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;else&lt;/SPAN&gt; Newfield&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, the data must be in order to permit the BY statement.&amp;nbsp; In your sample data, that is not a problem.&amp;nbsp; But sometimes you need to run PROC SORT first.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 14:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-in-last-row/m-p/516314#M139442</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-11-27T14:24:43Z</dc:date>
    </item>
  </channel>
</rss>

