<?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 Maximum value for chacracter variables in ID statement in PROC MEANS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/724982#M225118</link>
    <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yesterday, I learned how to tag along a variable to proc means by using ID statement as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;. However, the description for &lt;A href="https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000146733.htm" target="_self"&gt;this one&lt;/A&gt; is quite ambiguous (without example).&lt;/P&gt;
&lt;P&gt;Therefore, I did another search and see&lt;A href="https://stackoverflow.com/questions/20917534/what-is-the-function-of-an-id-statement-in-proc-means-in-sas#:~:text=The%20proc%20means%20procedure%20can,and%20output%20that%20summary%20statistics.&amp;amp;text=By%20using%20ID%20statement%20with,ID%20within%20the%20by%20group." target="_self"&gt; there is a discussion&lt;/A&gt; having the example and quite nice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the sashelp.class dataset for this example&lt;/P&gt;
&lt;P&gt;The dataset is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Name	Sex	Age	Height	Weight
Joyce	F	11	51.3	50.5
Thomas	M	11	57.5	85
Jane	F	12	59.8	84.5
Louise	F	12	56.3	77
James	M	12	57.3	83
John	M	12	59	    99.5
Robert	M	12	64.8	128
Alice	F	13	56.5	84
Barbara	F	13	65.3	98
Jeffrey	M	13	62.5	84
Carol	F	14	62.8	102.5
Judy	F	14	64.3	90
Alfred	M	14	69	    112.5
Henry	M	14	63.5	102.5
Janet	F	15	62.5	112.5
Mary	F	15	66.5	112
Ronald	M	15	67	    133
William	M	15	66.5	112
Philip	M	16	72	    150&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My code is as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  set sashelp.class;
run;

proc sort data= have;
by age;
run;

proc means data=have;
by age;
var Height Weight;
id Sex;
output out=means(drop =_type_ _freq_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As what I understand, The value of variable Sex will follow the value of variable Age (if there are two vars in IDs, the second var will follow the maximum value of the first var (same row))&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;identifies one or more variables from the input data set whose maximum values for groups of observations PROC MEANS includes in the output data set&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;note that "maximum" refers to alphabetical order in case of character variables such as LOC&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But, the result from running the code above is&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_1-1615322999781.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55748iB46477D5EC342BB0/image-size/large?v=v2&amp;amp;px=999" role="button" title="My97_1-1615322999781.png" alt="My97_1-1615322999781.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have two curiousities here:&lt;/P&gt;
&lt;P&gt;1. I confused because the value shown in Sex should be "F" rather than "M:" (because 'F' standing in front of 'M' in alphabet. Even it does not relate to the calculation in general, but because I want to apply in my case with the variable LOC, so I need things precisely.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. The ID statement seems to be efficiently used in a case that the character variables are constant over time like in &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-tag-along-one-more-variable-to-ouput-in-proc-means/m-p/724808#M225060" target="_self"&gt;my previous topic&lt;/A&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warmest regards.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Mar 2021 20:55:06 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-03-09T20:55:06Z</dc:date>
    <item>
      <title>Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/724982#M225118</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yesterday, I learned how to tag along a variable to proc means by using ID statement as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;. However, the description for &lt;A href="https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000146733.htm" target="_self"&gt;this one&lt;/A&gt; is quite ambiguous (without example).&lt;/P&gt;
&lt;P&gt;Therefore, I did another search and see&lt;A href="https://stackoverflow.com/questions/20917534/what-is-the-function-of-an-id-statement-in-proc-means-in-sas#:~:text=The%20proc%20means%20procedure%20can,and%20output%20that%20summary%20statistics.&amp;amp;text=By%20using%20ID%20statement%20with,ID%20within%20the%20by%20group." target="_self"&gt; there is a discussion&lt;/A&gt; having the example and quite nice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the sashelp.class dataset for this example&lt;/P&gt;
&lt;P&gt;The dataset is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Name	Sex	Age	Height	Weight
Joyce	F	11	51.3	50.5
Thomas	M	11	57.5	85
Jane	F	12	59.8	84.5
Louise	F	12	56.3	77
James	M	12	57.3	83
John	M	12	59	    99.5
Robert	M	12	64.8	128
Alice	F	13	56.5	84
Barbara	F	13	65.3	98
Jeffrey	M	13	62.5	84
Carol	F	14	62.8	102.5
Judy	F	14	64.3	90
Alfred	M	14	69	    112.5
Henry	M	14	63.5	102.5
Janet	F	15	62.5	112.5
Mary	F	15	66.5	112
Ronald	M	15	67	    133
William	M	15	66.5	112
Philip	M	16	72	    150&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My code is as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  set sashelp.class;
run;

proc sort data= have;
by age;
run;

proc means data=have;
by age;
var Height Weight;
id Sex;
output out=means(drop =_type_ _freq_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As what I understand, The value of variable Sex will follow the value of variable Age (if there are two vars in IDs, the second var will follow the maximum value of the first var (same row))&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;identifies one or more variables from the input data set whose maximum values for groups of observations PROC MEANS includes in the output data set&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;note that "maximum" refers to alphabetical order in case of character variables such as LOC&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But, the result from running the code above is&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_1-1615322999781.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55748iB46477D5EC342BB0/image-size/large?v=v2&amp;amp;px=999" role="button" title="My97_1-1615322999781.png" alt="My97_1-1615322999781.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have two curiousities here:&lt;/P&gt;
&lt;P&gt;1. I confused because the value shown in Sex should be "F" rather than "M:" (because 'F' standing in front of 'M' in alphabet. Even it does not relate to the calculation in general, but because I want to apply in my case with the variable LOC, so I need things precisely.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. The ID statement seems to be efficiently used in a case that the character variables are constant over time like in &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-tag-along-one-more-variable-to-ouput-in-proc-means/m-p/724808#M225060" target="_self"&gt;my previous topic&lt;/A&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warmest regards.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 20:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/724982#M225118</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-09T20:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/724985#M225120</link>
      <description>&lt;P&gt;RTM. &lt;BR /&gt;ID -&amp;gt; identifies one or more variables from the input data set whose &lt;STRONG&gt;maximum values for groups of observations PROC MEANS includes in the output data set&lt;/STRONG&gt;.&lt;BR /&gt;&lt;BR /&gt;You likely need to remerge your data, ID identifies the SEX with the maximum value. If the value is not consistent within the BY groups and you're expecting the value from the original data then you must remerge instead of use ID. &lt;BR /&gt;&lt;STRONG&gt;Or look at the IDMIN or PRINTIDVARS option on the PROC statement as the documentation suggests.&lt;/STRONG&gt; &lt;BR /&gt;&lt;BR /&gt;How to remerge:&lt;BR /&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank" rel="noopener"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Documentation&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=n1niocuk07q2wnn16mz96fboupgc.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=n1niocuk07q2wnn16mz96fboupgc.htm&amp;amp;locale=en&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;PROC SUMMARY offers some other ID options but in general, I prefer to just remerge as it's less testing or worrying.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 21:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/724985#M225120</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-09T21:14:17Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725000#M225124</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for your suggestion&lt;/P&gt;
&lt;P&gt;Let's say about the github solution, I deal with the Solution 1&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Add average value to a dataset;
*Solution 1 - PROC MEANS + Data step;
******************************************************;

proc means data=sashelp.class noprint;
    output out=avg_values mean(height)=avg_height;
run;

data class_data;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;

proc print data=class_data;/* I adjusted this one from class to class_data because dataset class does not exist*/
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is as below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1615329342541.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55752i3D315CCBD56AA247/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1615329342541.png" alt="My97_0-1615329342541.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;It is a desired result, but I do not know how SAS put avg_height to every row like that. What I focus on is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class_data;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From such a code, we only have one time that _n_1 at the first iteration. So, avg_height should only display at the first row, and "." afterwards.&lt;/P&gt;
&lt;P&gt;I know that my imagined running maybe wrong but could you please explain it to me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apart from that, could you please tell me what RTM stand for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warmest regards.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 22:38:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725000#M225124</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-09T22:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725006#M225126</link>
      <description>RTM means Read The Manual or documentation as it's called these days. &lt;BR /&gt;&lt;BR /&gt;IF _N_=1 trick means you need to understand how the SAS PDV works with multiple SET statements.&lt;BR /&gt;There are many papers out there on how the PDV works including in the documentation (RTM).&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Mar 2021 22:55:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725006#M225126</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-09T22:55:55Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725013#M225130</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;1. I confused because the value shown in Sex should be "F" rather than "M:" (because 'F' standing in front of 'M' in alphabet.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, this is not how the order of character variables is defined in SAS. For SAS &lt;FONT face="courier new,courier"&gt;'A' &amp;lt; 'B' &amp;lt; 'C' &amp;lt;&lt;/FONT&gt; ... like&amp;nbsp; &lt;FONT face="courier new,courier"&gt;1 &amp;lt; 2 &amp;lt; 3 &amp;lt;&lt;/FONT&gt; ... More precisely, SAS (under Windows) uses the underlying numeric ASCII codes to define the order of character values. As a consequence, lowercase letters are "greater than" uppercase letters (e.g., &lt;FONT face="courier new,courier"&gt;'Z' &amp;lt; 'a'&lt;/FONT&gt; is true), which in turn are "greater than" digits. See an ASCII table or play with the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0m5k2s76pmv9pn1n1lu3vfyq8s4.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;RANK&lt;/A&gt; or&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1jfxetjdn0nxan1w4e64c7rouz9.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;BYTE function&lt;/A&gt;&amp;nbsp;to find out more details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the "maximum" in the context of the ID statement in PROC MEANS refers to the value that comes &lt;EM&gt;last&lt;/EM&gt; according to the ASCII collating sequence. In your example each of the age groups contains at least one male person and since &lt;FONT face="courier new,courier"&gt;'M' &amp;gt; 'F'&lt;/FONT&gt; all age groups are "identified" by &lt;FONT face="courier new,courier"&gt;Sex='M'&lt;/FONT&gt;. Be creative and modify the example by assigning random letters (combine the BYTE and RAND('integer',...) functions) to variable &lt;FONT face="courier new,courier"&gt;Sex&lt;/FONT&gt; and see how the results change.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 23:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725013#M225130</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-09T23:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value for chacracter variables in ID statement in PROC MEANS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725014#M225131</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class_data;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From such a code, we only have one time that _n_1 at the first iteration. So, avg_height should only display at the first row, and "." afterwards.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Remember that variables read with a SET statement (here: &lt;FONT face="courier new,courier"&gt;avg_height&lt;/FONT&gt; from the second SET statement) are automatically &lt;EM&gt;retained&lt;/EM&gt;, i.e., they are &lt;EM&gt;not&lt;/EM&gt; reset to missing when the DATA step iterates. See section &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=en#p0wmd1lm606r67n1m6somosa8p0y" target="_blank" rel="noopener"&gt;Redundancy&lt;/A&gt; in the documentation of the RETAIN statement.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 23:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-for-chacracter-variables-in-ID-statement-in-PROC/m-p/725014#M225131</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-09T23:50:48Z</dc:date>
    </item>
  </channel>
</rss>

