<?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: How can I recode value in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13056#M52</link>
    <description>Hi.&lt;BR /&gt;
It looks like a little question reduce a big problem.&lt;BR /&gt;
Be honest, It spend a little much time to think about it, I underestimate its complex degree.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data test ;&lt;BR /&gt;
input GROUP CNT ;&lt;BR /&gt;
datalines ;&lt;BR /&gt;
0 32&lt;BR /&gt;
1 29&lt;BR /&gt;
2 14&lt;BR /&gt;
3 12&lt;BR /&gt;
4 13&lt;BR /&gt;
5 6&lt;BR /&gt;
6 4&lt;BR /&gt;
7 48&lt;BR /&gt;
8 18&lt;BR /&gt;
9 24&lt;BR /&gt;
10 34&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set test;&lt;BR /&gt;
 sum+cnt; output;&lt;BR /&gt;
 if sum gt 20 then sum=0;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 retain newgroup;&lt;BR /&gt;
 if _n_ eq 1 then newgroup=group;&lt;BR /&gt;
 if lag(sum) gt 20 then newgroup=group;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
    <pubDate>Mon, 21 Feb 2011 04:58:14 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-02-21T04:58:14Z</dc:date>
    <item>
      <title>How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13051#M47</link>
      <description>I have a dataset with two variables GROUP and CNT.  How can I recode the GROUP to NEWGROUP like this : &lt;BR /&gt;
if CNT&amp;gt;20, NEWGROUP=GROUP&lt;BR /&gt;
otherwise, store the GROUP (e.g. in GROUP 2) then sum the value of the CNT to next row until the summation result &amp;gt;20 (14+12), NEWGROUP=stored GROUP  (i.e. 2).&lt;BR /&gt;
&lt;BR /&gt;
data test ;&lt;BR /&gt;
input GROUP CNT ;&lt;BR /&gt;
datalines ;&lt;BR /&gt;
0	32	&lt;BR /&gt;
1	29	&lt;BR /&gt;
2	14	&lt;BR /&gt;
3	12	&lt;BR /&gt;
4	13	&lt;BR /&gt;
5	6	&lt;BR /&gt;
6	4	&lt;BR /&gt;
7	48	&lt;BR /&gt;
8	18	&lt;BR /&gt;
9	24	&lt;BR /&gt;
10	34	&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
The output data : &lt;BR /&gt;
&lt;BR /&gt;
GROUP	CNT	NEWGROUP&lt;BR /&gt;
0	32	0&lt;BR /&gt;
1	29	1&lt;BR /&gt;
2	14	2&lt;BR /&gt;
3	12	2&lt;BR /&gt;
4	13	4&lt;BR /&gt;
5	6	4&lt;BR /&gt;
6	4	4&lt;BR /&gt;
7	48	7&lt;BR /&gt;
8	18	8&lt;BR /&gt;
9	24	8&lt;BR /&gt;
10	34	10&lt;BR /&gt;
&lt;BR /&gt;
Thanks and regards,&lt;BR /&gt;
&lt;BR /&gt;
FC</description>
      <pubDate>Fri, 18 Feb 2011 16:02:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13051#M47</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-18T16:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13052#M48</link>
      <description>Sorry, have to admit a little confusion from my reading.&lt;BR /&gt;
It’s easy enough to keep a running total of CNT from row to row by retaining the variable and setting back to zero when necessary.  I’m just a bit confused by the logic.&lt;BR /&gt;
For example,  with row 3 in the data,  CNT on that row is less than 20, so what do you want in NEWGROUP?&lt;BR /&gt;
Then we get to row 4, so I assume we want sum the value of CNT for that row and the previous row (14 + 12) which is now &amp;gt; 20, so what do you want for NEWGROUP?  The value of group from the previous row?  Do we reset the counting of row so that with the next row we start over?</description>
      <pubDate>Fri, 18 Feb 2011 16:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13052#M48</guid>
      <dc:creator>nar_sas</dc:creator>
      <dc:date>2011-02-18T16:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13053#M49</link>
      <description>Based on our current understandingof the problem, this code should work, give it a try.  &lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
input GROUP CNT ;&lt;BR /&gt;
retain cnt2 0;&lt;BR /&gt;
retain newgroup 0;&lt;BR /&gt;
retain prevgroup 0;&lt;BR /&gt;
retain accumflag 0;&lt;BR /&gt;
keep group cnt newgroup;&lt;BR /&gt;
&lt;BR /&gt;
cnt2 = cnt2 + cnt;&lt;BR /&gt;
if (cnt &amp;gt; 20 or cnt2 &amp;gt; 20) then do;&lt;BR /&gt;
  if (accumflag = 0) then&lt;BR /&gt;
    newgroup = group;&lt;BR /&gt;
  else do;&lt;BR /&gt;
    newgroup = prevgroup;&lt;BR /&gt;
      prevgroup = 0;&lt;BR /&gt;
      accumflag = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  cnt2 = 0;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
else if (cnt2 &amp;lt;= 20) then do;&lt;BR /&gt;
  /* accumflag = zero, we are starting over and want to save the current group */&lt;BR /&gt;
  if accumflag = 0 then prevgroup = group;&lt;BR /&gt;
  accumflag = 1;&lt;BR /&gt;
  newgroup = prevgroup;&lt;BR /&gt;
end;&lt;BR /&gt;
  &lt;BR /&gt;
datalines ;&lt;BR /&gt;
0 32 &lt;BR /&gt;
1 29 &lt;BR /&gt;
2 14 &lt;BR /&gt;
3 12 &lt;BR /&gt;
4 13 &lt;BR /&gt;
5 6 &lt;BR /&gt;
6 4 &lt;BR /&gt;
7 48 &lt;BR /&gt;
8 18 &lt;BR /&gt;
9 24 &lt;BR /&gt;
10 34 &lt;BR /&gt;
;&lt;BR /&gt;
run;</description>
      <pubDate>Fri, 18 Feb 2011 17:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13053#M49</guid>
      <dc:creator>nar_sas</dc:creator>
      <dc:date>2011-02-18T17:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13054#M50</link>
      <description>Thanks for your reply.  I tried it out. &lt;BR /&gt;
&lt;BR /&gt;
The program result I get :&lt;BR /&gt;
&lt;BR /&gt;
GROUP	CNT	newgroup&lt;BR /&gt;
0	32	0&lt;BR /&gt;
1	29	1&lt;BR /&gt;
2	14	1&lt;BR /&gt;
3	12	3&lt;BR /&gt;
4	13	3&lt;BR /&gt;
5	6	3&lt;BR /&gt;
6	4	6&lt;BR /&gt;
7	48	7&lt;BR /&gt;
8	18	7&lt;BR /&gt;
9	24	9&lt;BR /&gt;
10	34	10&lt;BR /&gt;
11	5	10&lt;BR /&gt;
12	4	10&lt;BR /&gt;
13	3	10&lt;BR /&gt;
14	12	14&lt;BR /&gt;
15	2	14&lt;BR /&gt;
 &lt;BR /&gt;
Kindly advise how to fix the GROUP 6 in row 7.&lt;BR /&gt;
&lt;BR /&gt;
Thanks.&lt;BR /&gt;
&lt;BR /&gt;
FC</description>
      <pubDate>Sun, 20 Feb 2011 13:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13054#M50</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-20T13:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13055#M51</link>
      <description>Hi:&lt;BR /&gt;
  It looks like your code got truncated because of the &amp;lt; and/or &amp;gt; symbols in the code. When you make postings of code, these 2 symbols in particular seem to confusing the posting software because they are interpreted as belonging to HTML tags and not to syntax.&lt;BR /&gt;
 &lt;BR /&gt;
  When you cut and paste code into the forum, you may need to make a quick pass through and find/replace all &amp;lt; symbols with &amp;amp;lt; and all &amp;gt; symbols with &amp;amp;gt; prior to posting.&lt;BR /&gt;
 &lt;BR /&gt;
  Other useful tips for posting code, such as preserving indention levels and blank lines, etc, are contained in this previous forum post:&lt;BR /&gt;
 &lt;A href="http://support.sas.com/forums/thread.jspa?messageID=27609毙" target="_blank"&gt;http://support.sas.com/forums/thread.jspa?messageID=27609毙&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Sun, 20 Feb 2011 17:45:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13055#M51</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-02-20T17:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13056#M52</link>
      <description>Hi.&lt;BR /&gt;
It looks like a little question reduce a big problem.&lt;BR /&gt;
Be honest, It spend a little much time to think about it, I underestimate its complex degree.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data test ;&lt;BR /&gt;
input GROUP CNT ;&lt;BR /&gt;
datalines ;&lt;BR /&gt;
0 32&lt;BR /&gt;
1 29&lt;BR /&gt;
2 14&lt;BR /&gt;
3 12&lt;BR /&gt;
4 13&lt;BR /&gt;
5 6&lt;BR /&gt;
6 4&lt;BR /&gt;
7 48&lt;BR /&gt;
8 18&lt;BR /&gt;
9 24&lt;BR /&gt;
10 34&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set test;&lt;BR /&gt;
 sum+cnt; output;&lt;BR /&gt;
 if sum gt 20 then sum=0;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 retain newgroup;&lt;BR /&gt;
 if _n_ eq 1 then newgroup=group;&lt;BR /&gt;
 if lag(sum) gt 20 then newgroup=group;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 21 Feb 2011 04:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13056#M52</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-21T04:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I recode value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13057#M53</link>
      <description>This solution should do it with a single pass through the data.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data newgroup (drop=runningtotal);&lt;BR /&gt;
 set test;&lt;BR /&gt;
 retain newgroup runningtotal;&lt;BR /&gt;
 /* first observation, so initialize running total and newgroup */&lt;BR /&gt;
 if _N_ = 1 then do;&lt;BR /&gt;
  newgroup = group;&lt;BR /&gt;
  runningtotal = cnt;&lt;BR /&gt;
 end;&lt;BR /&gt;
 else do;&lt;BR /&gt;
  /* if running total gt 20 then start a new group and reset running total */&lt;BR /&gt;
  if runningtotal gt 20 then do;&lt;BR /&gt;
   newgroup = group;&lt;BR /&gt;
   runningtotal = cnt;&lt;BR /&gt;
  end;&lt;BR /&gt;
  /* if running total le 20 retain current group and add current count to running total  */&lt;BR /&gt;
  else runningtotal = sum(runningtotal, cnt);&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 23 Feb 2011 09:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-recode-value/m-p/13057#M53</guid>
      <dc:creator>LaurentdeWalick</dc:creator>
      <dc:date>2011-02-23T09:38:48Z</dc:date>
    </item>
  </channel>
</rss>

