<?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 calculate geodesic distance in SAS in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267520#M58040</link>
    <description>&lt;P&gt;Please provide a sample data set. Based on your requirement this may be you are looking for&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input x;&lt;BR /&gt;datalines;&lt;BR /&gt;2&lt;BR /&gt;4&lt;BR /&gt;6&lt;BR /&gt;8&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;y=lag(x)*0.75+x/2;&lt;BR /&gt;if _n_=1 then y=x/2;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 02 May 2016 00:36:11 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2016-05-02T00:36:11Z</dc:date>
    <item>
      <title>How to calculate new variable n using the new variable n-1??</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267516#M58036</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to calculate a new variable WANT(n), based on the previous observation's variable WANT(n-1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if n=1, WANT=HAVE/2;&lt;/P&gt;&lt;P&gt;else if n&amp;gt;1 then WANT(n)=WANT(n-1)*0.75+HAVE/2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have no idea which code to start. Could anyone give me some idea? Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 00:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267516#M58036</guid>
      <dc:creator>OceanDream</dc:creator>
      <dc:date>2016-05-02T00:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267517#M58037</link>
      <description>&lt;P&gt;I'm not sure if your subject (geodesic distance) is what you meant in your question. &amp;nbsp;If so:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check out this communities article: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-calculate-geodesic-distance-in-SAS/ta-p/241632" target="_self"&gt;How to calculate geodesic distance in SAS&lt;/A&gt;&amp;nbsp;by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13759"&gt;@MikeZdeb﻿&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it sounds like you need an approach that uses the LAG function (to store the value from a current obs to calculate a new value in the next record). &amp;nbsp;There are lots of papers and articles about this -- &lt;A href="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf" target="_self"&gt;here is one&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 00:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267517#M58037</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-05-02T00:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267518#M58038</link>
      <description>&lt;P&gt;Chris,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. I changed my title. It was automatically filled in by sas when I typed my title.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is not only a LAG function question. I think it's more of a LOOP function question, but I don't know how to use it, since the lagged variable I need to use to construct my new variable also need to be constructed the same way, because it's the same new variable, if this makes sense.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Haimeng&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 00:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267518#M58038</guid>
      <dc:creator>OceanDream</dc:creator>
      <dc:date>2016-05-02T00:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate new variable n using the new variable n-1??</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267519#M58039</link>
      <description>&lt;P&gt;I think that the implicit loop in the DATA step takes care of this for you. &amp;nbsp;Here's a simple example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set sashelp.class(rename=(weight=have) keep=weight);  
  prev_have=lag(have);
  if _n_ = 1 then want=have/2;
  else want=prev_have*0.75 + have/2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run that and check the math to see if it's what you want. &amp;nbsp;Note that you must always load up prev_have using the lag function, unconditionally . &amp;nbsp;That makes the prev obs value available for calculation in the next iteration.&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 00:35:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267519#M58039</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-05-02T00:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267520#M58040</link>
      <description>&lt;P&gt;Please provide a sample data set. Based on your requirement this may be you are looking for&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input x;&lt;BR /&gt;datalines;&lt;BR /&gt;2&lt;BR /&gt;4&lt;BR /&gt;6&lt;BR /&gt;8&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;y=lag(x)*0.75+x/2;&lt;BR /&gt;if _n_=1 then y=x/2;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 00:36:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267520#M58040</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-05-02T00:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267527#M58041</link>
      <description>&lt;P&gt;stat_sas, what I need is Y&lt;SPAN&gt;=lag(Y)*0.75+x/2, not&amp;nbsp;y=lag(x)*0.75+x/2. That's my problem &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 02:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267527#M58041</guid>
      <dc:creator>OceanDream</dc:creator>
      <dc:date>2016-05-02T02:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267528#M58042</link>
      <description>&lt;P&gt;Post sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I think all you need is a retain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain y;


***rest of code;

y=y*0.75+x/2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 02:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267528#M58042</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-02T02:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate geodesic distance in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267534#M58045</link>
      <description>&lt;P&gt;As mentioned earlier by Reeza, retain statement will be required to get previous value of y for calcuation. Please try the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;retain y;&lt;BR /&gt;if _n_=1 then y=x/2;&lt;BR /&gt;else y=y*0.75+x/2;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 05:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-calculate-new-variable-n-using-the-new-variable-n-1/m-p/267534#M58045</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-05-02T05:23:45Z</dc:date>
    </item>
  </channel>
</rss>

