<?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: &amp;quot;Coalesce&amp;quot; a value across groups of observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441636#M282644</link>
    <description>&lt;P&gt;Assuming you want a data set with the percent of any success by group, success is always 0/1 as described then this may work:&lt;/P&gt;
&lt;PRE&gt;proc summary data=successes nway ;
   class group code;
   var success;
   output out=successmax max=;
run;

proc summary data=successmax nway;
   class group;
   var success;
   output out= perc mean=;
run;&lt;/PRE&gt;
&lt;P&gt;The value of Success in the final set Perc is decimal percentage so instead of 57% it has 0.5714285714. Display as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that use of Class variables to group the data doesn't require explicit sorting though the output data will be.&lt;/P&gt;
&lt;P&gt;You can use data set options to drop the _type_ and _freq_ variables. _freq_ may help with verifying the number of codes used to calculate the percentage if concerned.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Mar 2018 16:57:42 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-03-02T16:57:42Z</dc:date>
    <item>
      <title>"Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441619#M282643</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is some sample made up data to show what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data successes;
   input code group something success;
datalines;
1104 1 8 0
1104 1 7 0
1105 1 2 0
1105 1 3 1
1105 1 4 0
1106 1 5 0
1106 1 6 1
1106 1 7 0
1106 1 8 0
1106 1 9 0
1106 1 1 0
1107 1 1 0
1107 1 3 0
1107 1 5 0
1107 1 7 0
1107 1 9 1
1108 1 2 0
1109 1 4 0
1109 1 6 0
1110 1 3 1
1110 1 2 1
1110 1 1 1
1111 2 2 0
1111 2 3 1
1111 2 4 0
1112 2 5 0
1112 2 6 1
1112 2 7 0
1112 2 8 0
1112 2 9 0
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For this data set, there are a number of observations for each code (in the real data anywhere between 1 and 25). A "1" in success means that that code was successful. In this data, codes 1104, 1108, and 1109 had no success in group 1. 1105, 1106, 1107, and 1110 had success.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want, is to adjust the data in such a way that I can do a Proc Freq to come up with a success percent by group. Group 1 has a success rate of 4/7 = 57%.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My thoughts were that I would make a successflag of some sort that was a 1 if any observation of a code had a success value of 1. Then de-duplicate based on code and successflag to create a data set I could run the FREQ against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mysuccesses;
	mysuccessflag = 0;
	if group = 1;
	do until (last.code);
		set successes;
		by code;
		if success = 1 then mysuccessflag = 1;
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but that is wrong in many ways. Looping in the data step error. Help here appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was trying to figure out how to loop through all the observations for a particular code and then set my flag to 1 if any observation had a success of 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I want a data set that looks like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data successes;
   input code group something success mysuccessflag;
datalines;
1104 1 8 0 0
1104 1 7 0 0
1105 1 2 0 1
1105 1 3 1 1
1105 1 4 0 1
1106 1 5 0 1
1106 1 6 1 1
1106 1 7 0 1
1106 1 8 0 1
1106 1 9 0 1
1106 1 1 0 1
1107 1 1 0 1
1107 1 3 0 1
1107 1 5 0 1
1107 1 7 0 1
1107 1 9 1 1
1108 1 2 0 0
1109 1 4 0 0
1109 1 6 0 0
1110 1 3 1 1
1110 1 2 1 1
1110 1 1 1 1
1111 2 2 0 1
1111 2 3 1 1
1111 2 4 0 1
1112 2 5 0 1
1112 2 6 1 1
1112 2 7 0 1
1112 2 8 0 1
1112 2 9 0 1
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With that I could &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = successes out = cleanedsuccesses nodupkey;
	by code;
run;

proc freq data = cleanedsuccesses;
	where group = 1;
	tables mysuccessflag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which gets me where I want to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I'm open to suggestions. I can do this with SQL but I want to do it something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 16:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441619#M282643</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-03-02T16:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: "Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441636#M282644</link>
      <description>&lt;P&gt;Assuming you want a data set with the percent of any success by group, success is always 0/1 as described then this may work:&lt;/P&gt;
&lt;PRE&gt;proc summary data=successes nway ;
   class group code;
   var success;
   output out=successmax max=;
run;

proc summary data=successmax nway;
   class group;
   var success;
   output out= perc mean=;
run;&lt;/PRE&gt;
&lt;P&gt;The value of Success in the final set Perc is decimal percentage so instead of 57% it has 0.5714285714. Display as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that use of Class variables to group the data doesn't require explicit sorting though the output data will be.&lt;/P&gt;
&lt;P&gt;You can use data set options to drop the _type_ and _freq_ variables. _freq_ may help with verifying the number of codes used to calculate the percentage if concerned.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 16:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441636#M282644</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-02T16:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: "Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441637#M282645</link>
      <description>&lt;P&gt;Try a DoW loop instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 16:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441637#M282645</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-02T16:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: "Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441655#M282646</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;summary&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=successes &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;nway&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;class&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; group code;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;var&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; success;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;output&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=successmax &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;max&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;So, we are summarizing the data by group and code.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;The nway option collapses that down to one observation per combo of group and code. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Output creates a file.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Max= is where I kind of get lost.&amp;nbsp; That somehow magically assigns the maximum value of the variable named (var = success) as the value of the summarized success variable.&amp;nbsp; I think. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;I don't understand it fully but it works.&amp;nbsp;&amp;nbsp; If I add&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FORMAT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; VALUE success_fmt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; 0 = "No"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;1 = "Yes";&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data = successmax;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;where group = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;format success success_fmt.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables success;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;working right off the first created data set I get&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                          The SAS System           09:10 Friday, March 2, 2018   8

                                        The FREQ Procedure

                                                       Cumulative    Cumulative
                   success    Frequency     Percent     Frequency      Percent
                   
                       No            3       42.86             3        42.86
                       Yes           4       57.14             7       100.00&lt;/CODE&gt;&lt;/PRE&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;&lt;LI-WRAPPER&gt;which gets me there.&amp;nbsp; &lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Very cool.&amp;nbsp; Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 17:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441655#M282646</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-03-02T17:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: "Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441667#M282647</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/6401"&gt;@HB&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;summary&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=successes &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;nway&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;class&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; group code;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;var&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; success;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;output&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=successmax &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;max&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;So, we are summarizing the data by group and code.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;The nway option collapses that down to one observation per combo of group and code. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Output creates a file.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Max= is where I kind of get lost.&amp;nbsp; That somehow magically assigns the maximum value of the variable named (var = success) as the value of the summarized success variable.&amp;nbsp; I think. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is exactly what the MAX statistic does, report the maximum value of the variable(s) within the combinations of the class variable(s).&lt;/P&gt;
&lt;P&gt;Since your description was sort of "I want a 'success' for the group/code combination if any of the individual records were a success" then one way to think of that is the maximum value.&lt;/P&gt;
&lt;P&gt;0/1 coded variables are very nice for use with some of the summary statistics: n is the number of valid values; mean is percent and sum is the number of 1 values (successes, valid, true, what have you for interpretation).&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 18:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441667#M282647</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-02T18:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: "Coalesce" a value across groups of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441925#M282648</link>
      <description>&lt;P&gt;Dow fun:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data successes;
   input code group something success;
datalines;
1104 1 8 0
1104 1 7 0
1105 1 2 0
1105 1 3 1
1105 1 4 0
1106 1 5 0
1106 1 6 1
1106 1 7 0
1106 1 8 0
1106 1 9 0
1106 1 1 0
1107 1 1 0
1107 1 3 0
1107 1 5 0
1107 1 7 0
1107 1 9 1
1108 1 2 0
1109 1 4 0
1109 1 6 0
1110 1 3 1
1110 1 2 1
1110 1 1 1
1111 2 2 0
1111 2 3 1
1111 2 4 0
1112 2 5 0
1112 2 6 1
1112 2 7 0
1112 2 8 0
1112 2 9 0
;
run;

data want;
_success=0;__success=0;
do until(last.group);
	_found=0;
	do until(last.code);
	set successes;
	by group code;
	if not _found and success=1 then _success+success;
	if success=1 then do;__success+success;_found=1;end;
	end;
	if last.group then success_pct=divide(_success,__success)*100;
end;
drop _: code something success;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Mar 2018 21:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/quot-Coalesce-quot-a-value-across-groups-of-observations/m-p/441925#M282648</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-02T21:33:39Z</dc:date>
    </item>
  </channel>
</rss>

