<?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: How to get minimum value for particular column group by with other column using data step in SAS Software for Learning Community</title>
    <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/846016#M825</link>
    <description>&lt;P&gt;MIN() works on a row, and takes the minimum for a series of variables. It does not answer the question you've asked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've illustrated how it would work in a data step, which also has problems as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;has indicated due to not accounting for missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the value added back into a data step to use in other operations, here are different ways to do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class;
quit;

******************************************************;
*Add average value to a dataset - with grouping variables;
*Solution 1 - PROC MEANS + Data step;
******************************************************;
proc means data=sashelp.class noprint nway;
class sex;
    output out=avg_values mean(height)=avg_height;
run;

*sort data before merge;
proc sort data=sashelp.class out=class;
by sex;
run;

data class_data;
 merge class avg_values;
 by sex;


run;

proc print data=class_data;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class
group by sex;
quit;
Footer
© 2022 GitHub, Inc.&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 23 Nov 2022 20:26:40 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-11-23T20:26:40Z</dc:date>
    <item>
      <title>How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845900#M817</link>
      <description>How to get minimum value for particular column by another column only using data step. I know how to get this using proc sql, proc means but i would like to use data step. How to get it. I have researched about it but didn't get clarity.</description>
      <pubDate>Wed, 23 Nov 2022 12:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845900#M817</guid>
      <dc:creator>SASCODERS</dc:creator>
      <dc:date>2022-11-23T12:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845902#M818</link>
      <description>&lt;P&gt;We would need some sample data, presented as working SAS data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am also curious why you need to do this in a DATA step, when the other solutions (PROC MEANS, PROC SQL) give correct answers with minimum effort.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 12:10:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845902#M818</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-23T12:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845905#M819</link>
      <description>We can use any of the data present in sas help library. Yes, it is easy with proc means and sql but would like try with data step also.</description>
      <pubDate>Wed, 23 Nov 2022 12:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845905#M819</guid>
      <dc:creator>SASCODERS</dc:creator>
      <dc:date>2022-11-23T12:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845976#M821</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/421825"&gt;@SASCODERS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;... but would like try with data step also.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? What is the benefit?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What have you tried so far?&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 16:36:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845976#M821</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-23T16:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845978#M822</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data  = sashelp.class out=class;
by sex age;
run;

data want;
set class;
if first.sex;
keep sex age;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class;
by sex;
run;

data want;
set class;
by sex;

if first.sex then min_age = age;
if age &amp;lt; min_age then min_age = age;
if last.sex then output;
keep sex min_age;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Two 'data step' methods.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/421825"&gt;@SASCODERS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How to get minimum value for particular column by another column only using data step. I know how to get this using proc sql, proc means but i would like to use data step. How to get it. I have researched about it but didn't get clarity.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 16:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845978#M822</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-23T16:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845994#M823</link>
      <description>&lt;P&gt;Sorry, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; but this is one of the reasons why I try to have people AVOID writing their own code to do things that SAS has already programmed for us users. Your code works on data that does not have missing values. If the data has missing values, it gives a missing value for the minimum, which I contend is the wrong answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have seen this happen at my job, where someone writes their own version of calculating a statistic, they get it wrong, but they don't realize it, and then this wrong code is used in production operations, and now this incorrect answer has real consequences.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 18:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845994#M823</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-23T18:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845999#M824</link>
      <description>I have tried&lt;BR /&gt;data out;&lt;BR /&gt;set data;&lt;BR /&gt;by colname&lt;BR /&gt;min_1 = min(colname);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;but it is not giving the min value of column.</description>
      <pubDate>Wed, 23 Nov 2022 19:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/845999#M824</guid>
      <dc:creator>SASCODERS</dc:creator>
      <dc:date>2022-11-23T19:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to get minimum value for particular column group by with other column using data step</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/846016#M825</link>
      <description>&lt;P&gt;MIN() works on a row, and takes the minimum for a series of variables. It does not answer the question you've asked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've illustrated how it would work in a data step, which also has problems as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;has indicated due to not accounting for missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the value added back into a data step to use in other operations, here are different ways to do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class;
quit;

******************************************************;
*Add average value to a dataset - with grouping variables;
*Solution 1 - PROC MEANS + Data step;
******************************************************;
proc means data=sashelp.class noprint nway;
class sex;
    output out=avg_values mean(height)=avg_height;
run;

*sort data before merge;
proc sort data=sashelp.class out=class;
by sex;
run;

data class_data;
 merge class avg_values;
 by sex;


run;

proc print data=class_data;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class
group by sex;
quit;
Footer
© 2022 GitHub, Inc.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Nov 2022 20:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/How-to-get-minimum-value-for-particular-column-group-by-with/m-p/846016#M825</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-23T20:26:40Z</dc:date>
    </item>
  </channel>
</rss>

