<?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: Comparing values in groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593553#M170356</link>
    <description>Is your 5th record in that table for switch_within correct? I'm not understanding why the ID=3, Value=2, Name=Apple has a switchwithin and record #5 does not.</description>
    <pubDate>Wed, 02 Oct 2019 21:28:19 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-10-02T21:28:19Z</dc:date>
    <item>
      <title>Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593527#M170336</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following table I'm trying to create Switch_from_start :&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;start_ind&lt;/STRONG&gt;: I created this value to secure when name = "Start" as the first item within the same ID after sorted in descending order.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data df;
  set df;
if name = "Start" then start_ind = 1;
run;

proc sort data = df; by ID Name descending start_id; run; 
/*In the table below Name is not sorted*/


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;switch_from_start&lt;/STRONG&gt; : any change in Value in comparing to the Value when name = "Start" within the same ID. So if a different Value within the same ID occurs, switch_from_start will be "yes" + start_value + later appeared Value that is different than the start Value. (refer to output example below)&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;&amp;nbsp;&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt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&lt;/TABLE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 02:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593527#M170336</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-10-03T02:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593538#M170345</link>
      <description>&lt;P&gt;What are the rules for generating switch_within and switch_from_start&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2019 20:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593538#M170345</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-02T20:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593540#M170347</link>
      <description>&lt;P&gt;switch_from_start : any change in Value in comparing to the Value when name = "Start" within the same ID. So if a different value within the same ID occurs, switch_from_start will be "yes" + start_value + later appeared value that is different than the start value. (see table for output example)&lt;BR /&gt;&lt;BR /&gt;switch_within: combine the before and after values within the same ID if a change occurs. (see table for output example)&lt;BR /&gt;&lt;BR /&gt;Are those what you are looking for?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2019 20:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593540#M170347</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-10-02T20:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593553#M170356</link>
      <description>Is your 5th record in that table for switch_within correct? I'm not understanding why the ID=3, Value=2, Name=Apple has a switchwithin and record #5 does not.</description>
      <pubDate>Wed, 02 Oct 2019 21:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593553#M170356</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-02T21:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593566#M170362</link>
      <description>Hi, I removed switch_within because there is no way I can sort the records following Name = "Start" within the same ID. (I cannot sort Name alphabetically)</description>
      <pubDate>Wed, 02 Oct 2019 22:22:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593566#M170362</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-10-02T22:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593599#M170379</link>
      <description>&lt;P&gt;Use a double DOW loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input ID $ Name $ value start_ind;
datalines; 
1 Start 1 1
1 apple 1 
1 pear 1
2 Start 2 1
2 apple 2
2 peach 2
2 pear 1
2 guava 1
2 banana 2
3 Start 1 1
3 apple 2
3 peach 2
3 pear 1
;

data want;
if 0 then set have; /* sets variable order */
length switch_from_start $10;
switch_from_start = 'no';
do until (last.id);
  set have;
  by id;
  if first.id
  then startval = value;
  else if startval ne value and switch_from_start = 'no'
  then switch_from_start = catx(' ','Yes,',cats(startval,'_',value));
end;
do until (last.id);
  set have;
  by id;
  output;
  switch_from_start = '';
end;
drop startval;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                                    switch_
                          start_    from_
 ID     Name     value      ind     start

 1     Start       1         1      no      
 1     apple       1         .              
 1     pear        1         .              
 2     Start       2         1      Yes, 2_1
 2     apple       2         .              
 2     peach       2         .              
 2     pear        1         .              
 2     guava       1         .              
 2     banana      2         .              
 3     Start       1         1      Yes, 1_2
 3     apple       2         .              
 3     peach       2         .              
 3     pear        1         .              
&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 05:43:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593599#M170379</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-03T05:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593601#M170381</link>
      <description>&lt;P&gt;PS note how I presented the example dataset in a data step with datalines. It allows everybody else to safely recreate your dataset exactly with just a simple copy/paste and submit. Please present data in that way in the future; it's not really complicated and speeds up finding a solution for you. Help us to help you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 05:46:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593601#M170381</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-03T05:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing values in groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593711#M170441</link>
      <description>Hi Kurt, I will be sure to do that next time. Thank you so much for the help.</description>
      <pubDate>Thu, 03 Oct 2019 13:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-values-in-groups/m-p/593711#M170441</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-10-03T13:33:49Z</dc:date>
    </item>
  </channel>
</rss>

