<?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 Computing minimum value of a variable in DATA STEP in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308407#M66209</link>
    <description>&lt;P&gt;This is simple. I want to get the minimum value of a variable (Qtr) in a (hopefully built-in) function within DATA STEP.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In essence, I want to use the lag function to compute values (var_out) within each Group. (Data is sorted ascending by Group then by Qtr). Something like the following doesn't work.&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;DATA data_out;
    MERGE data_in data_est;
    BY Group;

    IF Qtr &amp;gt; min(Qtr)+3 THEN DO;
            var_out = var_in -(_A_1*lag(res) + _A_2*lag2(res) +  _A_3*lag3(res));
    END;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 01 Nov 2016 01:14:46 GMT</pubDate>
    <dc:creator>Usagi</dc:creator>
    <dc:date>2016-11-01T01:14:46Z</dc:date>
    <item>
      <title>Computing minimum value of a variable in DATA STEP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308407#M66209</link>
      <description>&lt;P&gt;This is simple. I want to get the minimum value of a variable (Qtr) in a (hopefully built-in) function within DATA STEP.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In essence, I want to use the lag function to compute values (var_out) within each Group. (Data is sorted ascending by Group then by Qtr). Something like the following doesn't work.&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;DATA data_out;
    MERGE data_in data_est;
    BY Group;

    IF Qtr &amp;gt; min(Qtr)+3 THEN DO;
            var_out = var_in -(_A_1*lag(res) + _A_2*lag2(res) +  _A_3*lag3(res));
    END;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 01:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308407#M66209</guid>
      <dc:creator>Usagi</dc:creator>
      <dc:date>2016-11-01T01:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Computing minimum value of a variable in DATA STEP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308408#M66210</link>
      <description>&lt;P&gt;What I actually did was something more like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA data_out;
    MERGE data_in data_est;
    BY Group;

    var_out = var_in -(_A_1*lag(res) + _A_2*lag2(res) +  _A_3*lag3(res));

*To set to missing the first 3 values in each Group;
    IF Qtr &amp;lt; min(Qtr)+4 THEN DO;
        var_out=.
    END;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Nov 2016 01:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308408#M66210</guid>
      <dc:creator>Usagi</dc:creator>
      <dc:date>2016-11-01T01:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Computing minimum value of a variable in DATA STEP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308413#M66211</link>
      <description>&lt;P&gt;I'm guessing that the value of QTR isn't a key element here. &amp;nbsp;What you really need is to have at least 4 quarters of data so that the LAG functions return RES values for the same GROUP. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's the case, you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge data_in data_est;&lt;/P&gt;
&lt;P&gt;by group;&lt;/P&gt;
&lt;P&gt;if first.group then n_records=1;&lt;/P&gt;
&lt;P&gt;else n_records + 1;&lt;/P&gt;
&lt;P&gt;back1 = lag(res);&lt;/P&gt;
&lt;P&gt;back2 = lag2(res);&lt;/P&gt;
&lt;P&gt;back3 = lag3(res);&lt;/P&gt;
&lt;P&gt;if n_records &amp;gt; 3 then var_out = var_in - (_A_1*back1) + _A_2*back2 + _A_3*back3;&lt;/P&gt;
&lt;P&gt;drop back1 back2 back3 n_records;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's important for LAG to execute on every observation, or else it won't return the proper value.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 02:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308413#M66211</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-01T02:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: Computing minimum value of a variable in DATA STEP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308434#M66212</link>
      <description>&lt;P&gt;Their calculating an average here, but the idea for a minimum is similar.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/25/027.html" target="_blank"&gt;http://support.sas.com/kb/25/027.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 05:48:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Computing-minimum-value-of-a-variable-in-DATA-STEP/m-p/308434#M66212</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-01T05:48:49Z</dc:date>
    </item>
  </channel>
</rss>

