<?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: Updating Groups Within Dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761900#M241165</link>
    <description>&lt;P&gt;Is this what you're looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id color :$7.;
datalines;
1 Blue
1 Green
1 Red
2 Blue
2 Yellow
3 Green
3 Red
3 Yellow
;

data want;
	merge	have (in = a)
			have (in = b rename = (color = color_red) where = (color_red = "Red"));
	by 		id;
	
			if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I don't overwrite the original variable, but you could do that if you wanted. I just don't like doing that personally.&lt;/P&gt;
&lt;PRE&gt;Obs id color color_red 
1 1 Blue Red 
2 1 Green Red 
3 1 Red Red 
4 2 Blue   
5 2 Yellow   
6 3 Green Red 
7 3 Red Red 
8 3 Yellow Red 
&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Aug 2021 19:57:59 GMT</pubDate>
    <dc:creator>maguiremq</dc:creator>
    <dc:date>2021-08-16T19:57:59Z</dc:date>
    <item>
      <title>Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761894#M241161</link>
      <description>&lt;P&gt;I’m going to use a simple example to ask my question because I'm not sure how to articulate what I'm trying to do.&amp;nbsp; If I have small groups within a dataset such as the ID column below.&amp;nbsp; I want to check to see if any record within an ID group has the color red.&amp;nbsp; If it does, I want to make all the records in that ID group red.&amp;nbsp; So that all 3 records for ID 1 and 3 would change to red and ID 2 would be unchanged.&amp;nbsp; How would I do that in SAS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;ID &lt;/U&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;U&gt;Color&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Green&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Red&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Blue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Yellow&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Green&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Red&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Yellow&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any suggestion.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 19:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761894#M241161</guid>
      <dc:creator>BillSut</dc:creator>
      <dc:date>2021-08-16T19:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761900#M241165</link>
      <description>&lt;P&gt;Is this what you're looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id color :$7.;
datalines;
1 Blue
1 Green
1 Red
2 Blue
2 Yellow
3 Green
3 Red
3 Yellow
;

data want;
	merge	have (in = a)
			have (in = b rename = (color = color_red) where = (color_red = "Red"));
	by 		id;
	
			if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I don't overwrite the original variable, but you could do that if you wanted. I just don't like doing that personally.&lt;/P&gt;
&lt;PRE&gt;Obs id color color_red 
1 1 Blue Red 
2 1 Green Red 
3 1 Red Red 
4 2 Blue   
5 2 Yellow   
6 3 Green Red 
7 3 Red Red 
8 3 Yellow Red 
&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Aug 2021 19:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761900#M241165</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-08-16T19:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761901#M241166</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302431"&gt;@BillSut&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I’m going to use a simple example to ask my question because I'm not sure how to articulate what I'm trying to do.&amp;nbsp; If I have small groups within a dataset such as the ID column below.&amp;nbsp; I want to check to see if any record within an ID group has the color red.&amp;nbsp; If it does, I want to make all the records in that ID group red.&amp;nbsp; So that all 3 records for ID 1 and 3 would change to red and ID 2 would be unchanged.&amp;nbsp; How would I do that in SAS?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;ID &lt;/U&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;U&gt;Color&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Green&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Red&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Blue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Yellow&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Green&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Red&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Yellow&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for any suggestion.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way, note the data step to provide an actual data set that can be used with code.&lt;/P&gt;
&lt;PRE&gt;data have;
 input ID $ Color $;
datalines;
 1 Blue
 1 Green
 1 Red
 2 Blue
 2 Yellow
 3 Green
 3 Red
 3 Yellow
 ;


 Proc sql;
    create table want as
    select a.id, coalescec(b.color,a.color) as color
    from have as a
         left join
         (select distinct id,color from have
          where color='Red') as b
          on a.id=b.id
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;The Sql part selects the Id's that have Red somewhere in the&amp;nbsp; color variable using the (select distinct). The Join then matches those id to the basic data. The Coalescec function then returns the first value encountered in the two subsets. In the case where the ID did not have "red" then the B.color is missing and the A.color (from the base set) is the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have more than one value you are looking for then you would need to provide a more complicated example of the data - as a data step- and the expected results.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 19:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761901#M241166</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-16T19:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761907#M241170</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID colour $;
cards;
1         Blue
1         Green
1         Red
2         Blue
2         Yellow
3         Green
3         Red
3         Yellow
;;;;
run;

proc sort data=have;
by id colour;
run;


data red_list;
set have;
where colour="Red";
by ID;
if first.ID;
rename colour=master_colour;
run;

data want;
merge have red_list;
by ID;
final_colour = coalescec(master_colour, colour);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;solution but a data step approach instead. These are probably the best approaches given your question. It's possible to do this in a single step using DoW if efficiency becomes an issue but that's a more complex programming approach.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 20:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761907#M241170</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-16T20:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761974#M241192</link>
      <description>&lt;P&gt;A hash object could be used:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data want;
   set have;
   
   if _n_ = 1 then do;
      declare hash h(dataset: 'have(where= (Color = "Red"))');
      h.defineKey('id');
      h.defineData('color');
      h.defineDone();
   end;
   
   rc = h.find();
   drop rc;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maybe a bit to hefty for the problem &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 06:16:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/761974#M241192</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-08-17T06:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Groups Within Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/762062#M241235</link>
      <description>Thank you very much. As usual I was making the problem more complicated than it was. Your solution is simple but elegant and makes perfect sense.</description>
      <pubDate>Tue, 17 Aug 2021 13:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Updating-Groups-Within-Dataset/m-p/762062#M241235</guid>
      <dc:creator>BillSut</dc:creator>
      <dc:date>2021-08-17T13:57:37Z</dc:date>
    </item>
  </channel>
</rss>

