<?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: returning a variable's value in a given month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589483#M168614</link>
    <description>&lt;P&gt;Here's another way to retain the value of X (renamed to MULT), without running a separate data step, or issuing a RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=mult);
  if _n_=1 then set have (where=(put(EOM,yymmdd5.)='2018-12' rename=(x=mult));
  set have;
  y=mult*c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is to use a conditional SET statement.&amp;nbsp;&amp;nbsp; All variables in SET statements are automatically retained.&amp;nbsp; I repeat "&lt;EM&gt;All variables in SET statements are automatically retained.&lt;/EM&gt;".&amp;nbsp;&amp;nbsp; But typically they are populated with new values when the normal iteration of the data step re-invokes the same SET statement (or any other SET that obtains the variables in question).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But here, the "IF _N_=1" set statement reads a variable named MULT.&amp;nbsp; Since that SET is never executed again, the MULT variable is neither set to missing or overwritten with a new value, and is therefore available for use in all the subsequent observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp; in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;'s suggested code, this program assume exactly one obs for 2018-12.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
    <pubDate>Tue, 17 Sep 2019 20:46:29 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-09-17T20:46:29Z</dc:date>
    <item>
      <title>returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589352#M168558</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; I have a numeic timeseries variable&amp;nbsp;"x" from&amp;nbsp;&amp;nbsp;20701-04 to 2018-12. Lets say x =5 in 201812. I would like to create a variable "y" based just the 2018-12 value of x which is 5.&amp;nbsp;So y needs&amp;nbsp;to be equal to 5 at all&amp;nbsp;months.&amp;nbsp;How do I do this?&amp;nbsp;I looked into the put() function but was confused on how to use it for this. The months are in &amp;nbsp;yymmd. format.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589352#M168558</guid>
      <dc:creator>ubshams</dc:creator>
      <dc:date>2019-09-17T13:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589358#M168559</link>
      <description>&lt;P&gt;When you say&amp;nbsp;&lt;SPAN&gt;20701-04, what date does that represent?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also, are you able to post some example of how your data is structured and what your desired result looks like? Makes it easier to provide a usable code answer.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:33:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589358#M168559</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-17T13:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589360#M168561</link>
      <description>&lt;P&gt;Sorry, typo, I meant 2007-01, representing Jan 2007. And 2018-12 represents Dec 2018.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589360#M168561</guid>
      <dc:creator>ubshams</dc:creator>
      <dc:date>2019-09-17T13:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589361#M168562</link>
      <description>&lt;P&gt;Can you post some sample data so we can see how your data is structured?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589361#M168562</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-17T13:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589365#M168564</link>
      <description>&lt;P&gt;Sure:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EOM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x&amp;nbsp;&amp;nbsp; c&amp;nbsp; y&amp;nbsp;&lt;/P&gt;&lt;P&gt;2007-01&amp;nbsp; 1&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&lt;BR /&gt;2007-02&amp;nbsp; 3&amp;nbsp;&amp;nbsp; 3&lt;BR /&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;2018-12&amp;nbsp; 5&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;y needs to equal to 5 multiplied by a given value of c in a month.&lt;/P&gt;&lt;P&gt;so y in 2007-01 need to be 5*2 and y in 201812 need to be 5*1, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589365#M168564</guid>
      <dc:creator>ubshams</dc:creator>
      <dc:date>2019-09-17T13:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589366#M168565</link>
      <description>&lt;P&gt;I just made up some simple data. You can do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    do dt='30apr2017'd by 0 while (dt le '31dec2018'd);
        x=ifn(dt='31dec2018'd, 5, 1);
        c=2;
        output;
        dt=intnx('month', dt, 1, 'e');
    end;
    format dt yymmd.;
run;

data want;
    do until (lr1);
        set have end=lr1;
        if dt='31dec2018'd then _x=x;
    end;
    do until(lr2);
        set have end=lr2;
        y=_x*c;
        output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589366#M168565</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-17T13:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589367#M168566</link>
      <description>&lt;P&gt;There isn't any other grouping variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 and not eof then do;
     set have (where=(EOM='2018-12')) end=eof;
     dec2018_x = x;
     retain dec2018_x;
  end;
  set have;
  y=c*dec2018_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 14:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589367#M168566</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-17T14:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589369#M168568</link>
      <description>&lt;P&gt;The logic should be:&lt;/P&gt;
&lt;P&gt;1) Read the dataset, locate the obs with EOM=2018-12 and save the value of X int a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Consider two situations:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; - what if there is no such obs ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; - what if there are more than one such obs but with different X values ?&lt;BR /&gt;&amp;nbsp; &amp;nbsp;assuming there is only one such obs continue to step 2.&lt;/P&gt;
&lt;P&gt;2) Retain the value of X found and compute C for each obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code should like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data _null_;
 set have (where=(put(EOM,yymmdd5.)='2018-12');
      call symput('X', x);
run;

data want;
 set have;
       retain mult &amp;amp;x;
       y = mult *c;
      drop mult;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 14:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589369#M168568</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-09-17T14:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589379#M168570</link>
      <description>&lt;P&gt;Note that putting floating point numbers into macro variables can result in unintended rounding of the values.&amp;nbsp; In general it is best to keep the values in actual variables instead of storing it as text in a macro variable.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 14:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589379#M168570</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-17T14:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: returning a variable's value in a given month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589483#M168614</link>
      <description>&lt;P&gt;Here's another way to retain the value of X (renamed to MULT), without running a separate data step, or issuing a RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=mult);
  if _n_=1 then set have (where=(put(EOM,yymmdd5.)='2018-12' rename=(x=mult));
  set have;
  y=mult*c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is to use a conditional SET statement.&amp;nbsp;&amp;nbsp; All variables in SET statements are automatically retained.&amp;nbsp; I repeat "&lt;EM&gt;All variables in SET statements are automatically retained.&lt;/EM&gt;".&amp;nbsp;&amp;nbsp; But typically they are populated with new values when the normal iteration of the data step re-invokes the same SET statement (or any other SET that obtains the variables in question).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But here, the "IF _N_=1" set statement reads a variable named MULT.&amp;nbsp; Since that SET is never executed again, the MULT variable is neither set to missing or overwritten with a new value, and is therefore available for use in all the subsequent observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp; in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;'s suggested code, this program assume exactly one obs for 2018-12.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 20:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/returning-a-variable-s-value-in-a-given-month/m-p/589483#M168614</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-17T20:46:29Z</dc:date>
    </item>
  </channel>
</rss>

