<?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: arithmetic operations within a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436052#M108400</link>
    <description>&lt;P&gt;It doesn't seem useful to have&amp;nbsp;the RANGE in a single value. That's usually done when you want to use it later on in a calculation with most values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how you do it with average, change it to RANGE to have it work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 11 Feb 2018 03:24:47 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-11T03:24:47Z</dc:date>
    <item>
      <title>arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436031#M108384</link>
      <description>&lt;P&gt;I have multiple observations for each ID. I also have a variable called "score". I want to make a new variable "range" based on the values in "score" variable. Range is the difference between maximum and minimum values for score for each id. Below is what I have and what I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;score&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Score&amp;nbsp; &amp;nbsp; &amp;nbsp;average (this variable is the difference between the last score for an ID and the first score for the same ID)&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your consideration. I appreciate your help.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 01:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436031#M108384</guid>
      <dc:creator>Sinakian1</dc:creator>
      <dc:date>2018-02-11T01:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436034#M108386</link>
      <description>&lt;P&gt;Use PROC SUMMARY, which computes the RANGE for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=have;
    class id;
    var score;
    output out=_ranges_ range=range;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can merge this back to the original data set using BY ID;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 01:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436034#M108386</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-02-11T01:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436035#M108387</link>
      <description>&lt;P&gt;Many things don't make sense.&amp;nbsp; Why should the difference between the first and last be the same as the difference between the min and the max?&amp;nbsp; Why should the result appear on just the first observation for each ID?&amp;nbsp; Why should the new field be named AVERAGE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At any rate, here's a program that takes care of one interpretation of the question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; min_val = min(min_val, score);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; max_val = max(max_val, score);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;average = max_val - min_val;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; average= .;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop min_val max_val;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 01:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436035#M108387</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-11T01:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436038#M108388</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID      score;
datalines;
1         10
1         20
1         30
2         20
2         30
3         50
3         60
;

proc sql;
create table want as
select *,case when score = min(score) then range(score) else .  end as range
from have
group by id
order by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Feb 2018 01:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436038#M108388</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T01:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436052#M108400</link>
      <description>&lt;P&gt;It doesn't seem useful to have&amp;nbsp;the RANGE in a single value. That's usually done when you want to use it later on in a calculation with most values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how you do it with average, change it to RANGE to have it work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436052#M108400</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-11T03:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436075#M108412</link>
      <description>&lt;P&gt;Nice code &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;. But, for the sake of clarity,&amp;nbsp;I would prefer if you didn't use numbers in the &lt;EM&gt;order by&lt;/EM&gt; clause, especially when referring to * as a column list. It forces the reader to go back to the definition of the dataset to identify the variables.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 06:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436075#M108412</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-02-11T06:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: arithmetic operations within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436076#M108413</link>
      <description>&lt;P&gt;Thank you Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;and my apologies to OP&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/186677"&gt;@Sinakian1&lt;/a&gt;&amp;nbsp; please see the revised:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID      score;
datalines;
1         10
1         20
1         30
2         20
2         30
3         50
3         60
;

proc sql;
create table want as
select *,case when score = min(score) then range(score) else .  end as range
from have
group by id
order by id,score;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Feb 2018 06:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/arithmetic-operations-within-a-variable/m-p/436076#M108413</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T06:15:12Z</dc:date>
    </item>
  </channel>
</rss>

