<?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: Using the max of a variable to generate a new variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737718#M28814</link>
    <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;Note the data step that creates actual data set to code with (Hint.);&lt;/P&gt;
&lt;PRE&gt;data have;
   input var1  var2;
datalines;
2       10
3       20
5       10
1       25
7       20
;

proc sql;
   create table want as
   select a.var1, a.var2, a.var1/b.maxvar2 as ratio
   from have as a ,
        (select max(var2) as maxvar2 from have) as b
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Depending on other bits in code Proc SQL may reorder values.&lt;/P&gt;
&lt;P&gt;The above code uses&amp;nbsp; (select max(var2) as maxvar2 from have) to find the maximum value and creates and alias of B to reference the value set created. The "from have as a, " uses data set Have and matches each record in the referenced A alias with all the B values (only one that there is). The comma between to sets performs a cartesian join, the fancy term for matching all against all. Because there isn't any way to reduce this you will see a note in the log that the operation cannot be optimized.&lt;/P&gt;</description>
    <pubDate>Wed, 28 Apr 2021 20:03:16 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-04-28T20:03:16Z</dc:date>
    <item>
      <title>Using the max of a variable to generate a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737712#M28812</link>
      <description>&lt;P&gt;I am wondering if there is a way to generate a new variable using the max of an existing variable. For example, I may have a data like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1&amp;nbsp; var2&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I want to generate a new variable in this data set called var3 such that var3 = var1/max(var2). The resulting data set would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1&amp;nbsp; var2&amp;nbsp; var3&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp;.08&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp;.12&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp;.20&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&amp;nbsp; &amp;nbsp; &amp;nbsp;.04&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp;.28&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would I do this?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 19:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737712#M28812</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-04-28T19:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using the max of a variable to generate a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737717#M28813</link>
      <description>&lt;P&gt;SQL is pretty straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table example as
select *, age/max(age) as var3
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Other options include merging in the max and doing it via a data step or using a DoW loop. I'd consider a DoW loop a little beyond your skill set at the moment.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are examples of how to add in mean, which can easily be translated to a maximum.&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/326526"&gt;@raivester&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am wondering if there is a way to generate a new variable using the max of an existing variable. For example, I may have a data like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1&amp;nbsp; var2&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I want to generate a new variable in this data set called var3 such that var3 = var1/max(var2). The resulting data set would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1&amp;nbsp; var2&amp;nbsp; var3&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp;.08&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp;.12&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp;.20&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&amp;nbsp; &amp;nbsp; &amp;nbsp;.04&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp;.28&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would I do this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&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>Wed, 28 Apr 2021 20:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737717#M28813</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-28T20:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using the max of a variable to generate a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737718#M28814</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;Note the data step that creates actual data set to code with (Hint.);&lt;/P&gt;
&lt;PRE&gt;data have;
   input var1  var2;
datalines;
2       10
3       20
5       10
1       25
7       20
;

proc sql;
   create table want as
   select a.var1, a.var2, a.var1/b.maxvar2 as ratio
   from have as a ,
        (select max(var2) as maxvar2 from have) as b
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Depending on other bits in code Proc SQL may reorder values.&lt;/P&gt;
&lt;P&gt;The above code uses&amp;nbsp; (select max(var2) as maxvar2 from have) to find the maximum value and creates and alias of B to reference the value set created. The "from have as a, " uses data set Have and matches each record in the referenced A alias with all the B values (only one that there is). The comma between to sets performs a cartesian join, the fancy term for matching all against all. Because there isn't any way to reduce this you will see a note in the log that the operation cannot be optimized.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 20:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-the-max-of-a-variable-to-generate-a-new-variable/m-p/737718#M28814</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-28T20:03:16Z</dc:date>
    </item>
  </channel>
</rss>

