<?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: Proc sql: replace value with another value within group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743672#M232879</link>
    <description>&lt;P&gt;The data "want" is the output I am looking for. It should be based on data "have" with a new created variable called new_name.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
    <pubDate>Tue, 25 May 2021 19:04:32 GMT</pubDate>
    <dc:creator>sasecn</dc:creator>
    <dc:date>2021-05-25T19:04:32Z</dc:date>
    <item>
      <title>Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743659#M232872</link>
      <description>&lt;P&gt;I have a data with ids and names. Within one ID, there may have more than one values in Name. My goal is to create another variable called new_name. New_name will have the same value of name if all names are the same within one id, e.g. id1 in the example; will have a value of "multi_name" if ther are more than one values in Name, e.g. id 2 and 3 in the example; will have the value of name that is not equal to "no_name" wihtin the ID if there are two values within ID (one of them is "no_name") e.g. id 4 in the example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So id3 should have new_name=multi_name, but id4 should have new_name=g. The attached code can produce the sample data "have", and "want" is the one i am looking for. The proc sql is not finished, i don't know how to code it for id4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any help!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ name $;
datalines;
1 a
1 a
2 b
2 c
3 e
3 f
3 no_name
4 g
4 no_name
;
run;

data want;
input id $1. name $8. new_name $12.;
datalines;
1 a       a
1 a       a
2 b       multi_name
2 c       multi_name
3 e       multi_name
3 f       multi_name
3 no_name multi_name
4 g       g
4 no_name g
;
run;

proc sql;
create table want_1 as
select *,
case 
when count(distinct name) = 1 then name
when count(distinct name) &amp;gt; 1 then "multi_name"
/* how to code new_name = g for id 4 ? */
end as new_name
from have
group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 May 2021 18:33:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743659#M232872</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T18:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743665#M232876</link>
      <description>&lt;P&gt;I'm not quite sure if I understand what you're trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you mock up what your final output should look like?&amp;nbsp; If you post what you want your results to look like, then maybe I can help out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 18:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743665#M232876</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-05-25T18:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743672#M232879</link>
      <description>&lt;P&gt;The data "want" is the output I am looking for. It should be based on data "have" with a new created variable called new_name.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 19:04:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743672#M232879</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T19:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743673#M232880</link>
      <description>&lt;P&gt;Easy enough in PROC SQL since SAS will automatically remerge aggregate statistics like COUNT() and MIN() back onto all observations in a group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's replace those "no_name" values with actual empty strings so that SAS will treat them as missing values.&amp;nbsp; Let's add a unique ID so it will be easier to compare the values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  row+1;
  input id $ name $;
datalines;
1 a
1 a
2 b
2 c
3 e
3 f
3 .
4 g
4 .
;

data expect;
  row+1;
  input id $ name $ new_name :$12.;
datalines;
1 a       a
1 a       a
2 b       multi_name
2 c       multi_name
3 e       multi_name
3 f       multi_name
3 .       multi_name
4 g       g
4 .       g
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just group by ID and depending on whether the number of non-missing values is more than 1 decide whether to use that value (note that min() or max() of one value is the same value).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
  select a.*
       , case when (count(distinct name) &amp;gt; 1) then 'multi_name' 
         else min(name) end as new_name length=12
  from have a
  group by a.id 
  order by row
;
quit;

proc print data=want; run;
proc compare data=want compare=expect;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;SAS 9.4 on WINDOWS

Obs    row    id    name    new_name

 1      1     1      a      a
 2      2     1      a      a
 3      3     2      b      multi_name
 4      4     2      c      multi_name
 5      5     3      e      multi_name
 6      6     3      f      multi_name
 7      7     3             multi_name
 8      8     4      g      g
 9      9     4             g
&amp;#12;
SAS 9.4 on WINDOWS                                                                                     

The COMPARE Procedure
Comparison of WORK.WANT with WORK.EXPECT
(Method=EXACT)

Data Set Summary

Dataset               Created          Modified  NVar    NObs

WORK.WANT    25MAY21:15:06:21  25MAY21:15:06:21     4       9
WORK.EXPECT  25MAY21:15:06:21  25MAY21:15:06:21     4       9


Variables Summary

Number of Variables in Common: 4.


Observation Summary

Observation      Base  Compare

First Obs           1        1
Last  Obs           9        9

Number of Observations in Common: 9.
Total Number of Observations Read from WORK.WANT: 9.
Total Number of Observations Read from WORK.EXPECT: 9.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 9.

NOTE: No unequal values were found. All values compared are exactly equal.

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 19:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743673#M232880</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-25T19:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743677#M232882</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp; Here is another variant similar to Tom.&amp;nbsp; This actually nests IFC/IFN functions within a CASE WHEN expression taught by Guru Paul D&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp; a while ago-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ name $;
datalines;
1 a
1 a
2 b
2 c
3 e
3 f
3 no_name
4 g
4 no_name
;
run;

proc sql;
 create table want as
 select *, 
 case 
  when count(distinct name)=2 and max(name='no_name') then max(ifc(name='no_name',' ',name))
  when count(distinct name)&amp;gt;1  then 'multi_name'
  else name 
 end as new_name length=12
 from have
 group by id;
quit;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;name&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;new_name&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;b&lt;/TD&gt;
&lt;TD class="l data"&gt;multi_name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;c&lt;/TD&gt;
&lt;TD class="l data"&gt;multi_name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;no_name&lt;/TD&gt;
&lt;TD class="l data"&gt;multi_name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;f&lt;/TD&gt;
&lt;TD class="l data"&gt;multi_name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;e&lt;/TD&gt;
&lt;TD class="l data"&gt;multi_name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;no_name&lt;/TD&gt;
&lt;TD class="l data"&gt;g&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;g&lt;/TD&gt;
&lt;TD class="l data"&gt;g&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 19:14:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743677#M232882</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-05-25T19:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743679#M232884</link>
      <description>&lt;P&gt;Thanks, it works. Can you explain the condition "and max(name='no_name')" ? I understand the first part. Does the whole condition mean: if there are two outcomes of Name, one is some value of name, the other one is 'no_name', then take the max value (ie. not the emapty one)?&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 19:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743679#M232884</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T19:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743685#M232887</link>
      <description>&lt;P&gt;Thanks, this code works well. I just don't understand the "else min(name)" part. For example, how it can take the value of "g" in id4 case? Does the SAS just ingore the row with missing value?&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 19:58:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743685#M232887</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T19:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743692#M232892</link>
      <description>&lt;P&gt;Hi again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp; &amp;nbsp;the logic is fairly simple once we understand the "waterfall construct"-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Start with checking for 2 distinct values with one valid name and the other no_name viz.&amp;nbsp;max(name='no_name') results in a boolean 1's or 0's true for no_name or false&lt;/P&gt;
&lt;P&gt;2. 1st When fails, the 2nd when expression checks for any count of distinct names &amp;gt;1 and assigns the easy multiname&lt;/P&gt;
&lt;P&gt;3. When 1 &amp;amp; 2 fails, there can only be one more options i.e. all names being same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 20:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743692#M232892</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-05-25T20:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743694#M232894</link>
      <description>&lt;P&gt;MIN() of character strings ignores blank values the same as it ignores missing value for numeric variables.&lt;/P&gt;
&lt;P&gt;COUNT() also ignores the blank values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data really has "no_name" as the value of NAME instead of blanks then use CASE (or IFC()) to remove those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(distinct case when name='no_name' then ' ' else name end)
...
min(case when name='no_name' then ' ' else name end)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 May 2021 20:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743694#M232894</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-25T20:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743697#M232896</link>
      <description>&lt;P&gt;Thank you! Really helpful!&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 20:47:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743697#M232896</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T20:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: replace value with another value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743698#M232897</link>
      <description>&lt;P&gt;Great, thanks for the answer1&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 20:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-replace-value-with-another-value-within-group/m-p/743698#M232897</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-05-25T20:48:02Z</dc:date>
    </item>
  </channel>
</rss>

