<?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: Compare observations within groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293640#M61169</link>
    <description>&lt;P&gt;sorry for my previous post.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it should compare with immediately follow.&lt;/P&gt;&lt;P&gt;here "w2" values should compare with "w3" values.&lt;/P&gt;</description>
    <pubDate>Wed, 24 Aug 2016 07:46:42 GMT</pubDate>
    <dc:creator>Raj_C</dc:creator>
    <dc:date>2016-08-24T07:46:42Z</dc:date>
    <item>
      <title>Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293634#M61163</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;&lt;P&gt;The problem is how to compare observations within groups. My data set has a column named sub_id, which is the ID number of each subjects. Another column is fol_id, it is ID of each subject visit. Another column is value, it is the value of subject.&lt;/P&gt;&lt;P&gt;now we want to compare w3 values with w2, w4 values with w3. for e.g. if we compare w3 values with w2 then 12 &amp;amp; 14 are the new values so we want to show these to in the output, like this if we compare w4 values with w3 then 15 is the new value so here 15 we want to show in the output.&lt;/P&gt;&lt;P&gt;but&amp;nbsp;don't know how to&amp;nbsp;develop code.&amp;nbsp;Any suggestions and sample codes are highly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input sub_id $ fold_id $ value;&lt;BR /&gt;datalines;&lt;BR /&gt;abc w2 10&lt;/P&gt;&lt;P&gt;abc w2 11&lt;BR /&gt;abc w3 10&lt;BR /&gt;abc w3 14&lt;BR /&gt;abc w3 12&lt;BR /&gt;abc w3 11&lt;BR /&gt;abc w4 12&lt;BR /&gt;abc w4 15&lt;/P&gt;&lt;P&gt;abc w4 10&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 06:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293634#M61163</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T06:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293635#M61164</link>
      <description>&lt;P&gt;I can't infer a clear rule from what you posted.&lt;/P&gt;
&lt;P&gt;Which "w2" observation should be compared to which "w3" observation? Or are you just comparing those that immediately follow? Or sums of values?&lt;/P&gt;
&lt;P&gt;A "want" dataset could be heplful in clarifying your intentions.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 06:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293635#M61164</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-24T06:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293639#M61168</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes you are absolutely correct. "w2" observation should be compared to "w3" observation.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 07:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293639#M61168</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T07:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293640#M61169</link>
      <description>&lt;P&gt;sorry for my previous post.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it should compare with immediately follow.&lt;/P&gt;&lt;P&gt;here "w2" values should compare with "w3" values.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 07:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293640#M61169</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T07:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293641#M61170</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sub_id $ fold_id $ value;
datalines;
abc w2 10
abc w2 11
abc w3 10
abc w3 14
abc w3 12
abc w3 11
abc w4 12
abc w4 15
abc w4 10
;
run;

data want;
set have;
by sub_id fold_id;
compval = lag(value);
if first.sub_id or not first.fold_id then compval = .;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will give you the immediately preceding value whenever fold_id changes within a sub_id group.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 07:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293641#M61170</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-24T07:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293644#M61171</link>
      <description>&lt;P&gt;Thank you so much for your response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry if I misguide you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The actual qestion is total (not sum) "w2" values should compare with total (not sum) "w3" values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like if we compare "w3" total values with "w2" total values then 12 &amp;amp; 14 values are new (which are not in "w2") so these we need to identify.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 09:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293644#M61171</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T09:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293658#M61182</link>
      <description>&lt;PRE&gt;
OK.Assuming there are not missing value for VALUE.


data have;
input sub_id $ fold_id $ value;
datalines;
abc w2 10
abc w2 11
abc w3 10
abc w3 14
abc w3 12
abc w3 11
abc w4 12
abc w4 15
abc w4 10
;
run;
data want;
 array x{99999} _temporary_;
 group+1;
  
 do until(last.fold_id);
  set have;
  by sub_id fold_id;
  if value not in x and group ne 1 then output;
 end;
 
 call missing(of x{*});
 n=0;
 do until(last.fold_id);
  set have;
  by sub_id fold_id;
  n+1;
  x{n}=value;
 end;
 
drop n group ;
run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Aug 2016 10:38:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293658#M61182</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-24T10:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293664#M61187</link>
      <description>&lt;P&gt;Thank you so much for your immediate help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is working absoultely fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;Raju&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 10:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293664#M61187</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T10:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293665#M61188</link>
      <description>&lt;P&gt;So you want to find values in a new group that were not present as one value in the previous group.&lt;/P&gt;
&lt;P&gt;try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by sub_id fold_id;
retain old_list new_list;
length old_list new_list $500;
if first.sub_id then new_list = '';
if first.fold_id
then do;
  old_list = new_list;
  new_list = '';
end;
if old_list ne ' ' and find(old_list,strip(put(value,best.))) = 0 then output;
new_list = catx(' ',trim(new_list),strip(put(value,best.)));
drop old_list new_list;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Aug 2016 10:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293665#M61188</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-24T10:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293666#M61189</link>
      <description>&lt;P&gt;Thank you so much, you are awesome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Its working fine.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2016 10:53:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-observations-within-groups/m-p/293666#M61189</guid>
      <dc:creator>Raj_C</dc:creator>
      <dc:date>2016-08-24T10:53:51Z</dc:date>
    </item>
  </channel>
</rss>

