<?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: Loop in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899886#M83032</link>
    <description>&lt;P&gt;Hi Kurt_Bremser,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for helping me with this problem. Can you provide a code to generate the lead variable dinf(t+1), dinf(t+2), ..., dinf(t+5) using a loop?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Oct 2023 06:50:17 GMT</pubDate>
    <dc:creator>Golf</dc:creator>
    <dc:date>2023-10-25T06:50:17Z</dc:date>
    <item>
      <title>Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899870#M83027</link>
      <description>&lt;P&gt;Hi everyone, &lt;BR /&gt;I need help generating the first five lags of the first difference of inflation using a loop. However, I'm not getting any results for dinf_lag1 to dinf_lag5. Can someone assist me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data use;&lt;BR /&gt;set use;&lt;/P&gt;
&lt;P&gt;dinf = dif(inflation); /* Gerate first different of inflation */&lt;BR /&gt;array dinf_lag[5] ; /* Create an array of 5 variables */ &lt;BR /&gt;do i = 1 to 5;&lt;BR /&gt;%let i;&lt;BR /&gt;%put dinf_lag[&amp;amp;i] = lag&amp;amp;i(dinf); /* Generating variable names dinf_lag1,...,dinf_lag5 */&lt;BR /&gt;end;&lt;BR /&gt;drop i; /* Drop the loop counter variable from the final dataset */&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 05:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899870#M83027</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T05:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899875#M83028</link>
      <description>&lt;P&gt;The macro statements will execute before your data step and though won't be affected in any way by your do loop.&lt;/P&gt;
&lt;P&gt;Please provide some sample have data via working SAS datastep code and then show us the desired result for this sample data.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 05:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899875#M83028</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-25T05:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899883#M83029</link>
      <description>&lt;P&gt;Your code is equivalent to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let i;
%put dinf_lag[&amp;amp;i] = lag&amp;amp;i(dinf);

data use;
set use;
dinf = dif(inflation);
array dinf_lag[5];
do i = 1 to 5;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, the DATA step does nothing. Your first macro statement is invalid, as a %LET requires an equal sign, and the second will probably complain about the variable reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you seem to want five calls from LAG1 to LAG5, you either write those explicitly (no loop), or have them created in a %DO loop, for which you need to define a macro first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop;
%do i = 1 %to 5;
  dinf_lag[&amp;amp;i] = lag&amp;amp;i(dinf);
%end;
%mend;

data use1; /* don't overwrite the dataset, or you run the risk of destroying your work up to that point */
set use;
dinf = dif(inflation);
array dinf_lag[5];
%loop
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899883#M83029</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T06:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899884#M83030</link>
      <description>&lt;P&gt;Patrick,&lt;/P&gt;
&lt;P&gt;Thank you for your assistance. Here's the output from the code. Additionally, can you provide a code to generate the lead variable: dinf(t+1), dinf(t+2), ..., dinf(t+5)?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Golf_0-1698215763576.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89068i3FDD5B340C6B69DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Golf_0-1698215763576.png" alt="Golf_0-1698215763576.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the desired data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Golf_1-1698215905510.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89069iE19CE80D7E58F369/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Golf_1-1698215905510.png" alt="Golf_1-1698215905510.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:40:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899884#M83030</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T06:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899885#M83031</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Please provide some sample have data via working SAS datastep code&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I don't expect anyone being willing to re-engineer such source data for you based on a screenshot of a desired outcome. Please help us help you.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;We need such sample data to test whatever code we propose, we need the desired outcome and a narrative of the required logic to get from have to want. Plus actual sample have and want data also helps to remove a lot of ambiguity in narratives.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899885#M83031</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-25T06:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899886#M83032</link>
      <description>&lt;P&gt;Hi Kurt_Bremser,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for helping me with this problem. Can you provide a code to generate the lead variable dinf(t+1), dinf(t+2), ..., dinf(t+5) using a loop?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899886#M83032</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T06:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899887#M83033</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/313878"&gt;@Golf&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Kurt_Bremser,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for helping me with this problem. Can you provide a code to generate the lead variable dinf(t+1), dinf(t+2), ..., dinf(t+5) using a loop?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please supply example data in a working DATA step with DATALINES, and show the expected result.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899887#M83033</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T06:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899891#M83036</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my desired data for lead variables.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Golf_0-1698217792936.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89070i7EC59D039407BBD8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Golf_0-1698217792936.png" alt="Golf_0-1698217792936.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I use the simple codes as follows:&lt;/P&gt;
&lt;P&gt;proc expand data= ch6dols.time_series out=ch6dols.time_series;&lt;/P&gt;
&lt;P&gt;convert dinf = dinf_lead1 / transform=(lead 1); &lt;BR /&gt;convert dinf = dinf_lead2 / transform=(lead 2);&lt;BR /&gt;convert dinf = dinf_lead3 / transform=(lead 3);&lt;BR /&gt;convert dinf = dinf_lead4 / transform=(lead 4);&lt;BR /&gt;convert dinf = dinf_lead5 / transform=(lead 5);&lt;BR /&gt;convert dinf;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know how to generate this dataset in a loop. It would be helpful to generate longer leads and lags.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank You.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 07:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899891#M83036</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T07:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899893#M83037</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Please supply example data in a working &lt;U&gt;DATA&lt;/U&gt; step with &lt;U&gt;DATALINES&lt;/U&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need to see with what you start initially (the original ch6dols.time_series, before conversion).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;No&lt;/STRONG&gt; pictures, code text which we can copy to our program windows.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 07:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899893#M83037</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T07:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899894#M83038</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the codes:&lt;/P&gt;
&lt;P&gt;PROC IMPORT DATAFILE="/home/u45083887/my_content/SM518/SM518 Ch5/time_series_ex1.xlsx"&lt;BR /&gt;OUT=Ch6DOLS.time_series&lt;BR /&gt;DBMS=XLSX&lt;BR /&gt;REPLACE;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;data ch6dols.time_series;&lt;BR /&gt;set ch6dols.time_series;&lt;BR /&gt;dinf = dif(inflation);&lt;/P&gt;
&lt;P&gt;proc expand data= ch6dols.time_series out=ch6dols.time_series;&lt;/P&gt;
&lt;P&gt;convert dinf = dinf_lead1 / transform=(lead 1); &lt;BR /&gt;convert dinf = dinf_lead2 / transform=(lead 2);&lt;BR /&gt;convert dinf = dinf_lead3 / transform=(lead 3);&lt;BR /&gt;convert dinf = dinf_lead4 / transform=(lead 4);&lt;BR /&gt;convert dinf = dinf_lead5 / transform=(lead 5);&lt;BR /&gt;convert dinf;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc print data = ch6dols.time_series;&lt;BR /&gt;var dinf dinf_lead1 dinf_lead2 dinf_lead3 dinf_lead4 dinf_lead5;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Thank You.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 07:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899894#M83038</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T07:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899896#M83039</link>
      <description>&lt;P&gt;While having a cup of coffee, i repeat what as been asked twice already:&lt;/P&gt;
&lt;P&gt;&lt;FONT size="6"&gt;&lt;STRONG&gt;&lt;SPAN&gt;Please supply example data in a working &lt;U&gt;DATA&lt;/U&gt; step with &lt;U&gt;DATALINES&lt;/U&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;We don't have access to your Excel file and many of us won't open excel files attached to posts.&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 08:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899896#M83039</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-10-25T08:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899897#M83040</link>
      <description>Here it is.&lt;BR /&gt;data time_series;&lt;BR /&gt;input year inflation;&lt;BR /&gt;datalines;&lt;BR /&gt;1948 .&lt;BR /&gt;1949 -0.988480068&lt;BR /&gt;1950 1.053828352&lt;BR /&gt;1951 7.366171193&lt;BR /&gt;1952 2.227075801&lt;BR /&gt;1953 0.761468753&lt;BR /&gt;1954 0.351633428&lt;BR /&gt;1955 -0.262872574&lt;BR /&gt;1956 1.462744884&lt;BR /&gt;1957 3.277625189&lt;BR /&gt;1958 2.662459493&lt;BR /&gt;1959 0.909736704&lt;BR /&gt;1960 1.481626961&lt;BR /&gt;1961 1.058582189&lt;BR /&gt;1962 1.158617264&lt;BR /&gt;1963 1.236272118&lt;BR /&gt;1964 1.305567525&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Wed, 25 Oct 2023 08:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899897#M83040</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T08:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899900#M83042</link>
      <description>&lt;P&gt;When I combine your data with the picture of the wanted data, I think I see this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;you create, for every observation, the difference to the previous year&lt;/LI&gt;
&lt;LI&gt;then, you&amp;nbsp;&lt;EM&gt;look ahead&lt;/EM&gt; for this difference 1 to 5 years&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;To do this with DATA step logic, you first have to calculate the difference, and then do the look-ahead. One method for this look-ahead is to sort the intermediate dataset in reverse and use LAG1 to LAG5; another method is a 6-way MERGE of the intermediate dataset with itself, where you use increasing FIRSTOBS= options.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 09:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899900#M83042</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T09:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899913#M83043</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for your help. I will try your suggestion.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 10:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899913#M83043</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T10:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899926#M83044</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*It is IML thing*/
data time_series;
input year inflation;
datalines;
1948 .
1949 -0.988480068
1950 1.053828352
1951 7.366171193
1952 2.227075801
1953 0.761468753
1954 0.351633428
1955 -0.262872574
1956 1.462744884
1957 3.277625189
1958 2.662459493
1959 0.909736704
1960 1.481626961
1961 1.058582189
1962 1.158617264
1963 1.236272118
1964 1.305567525
;
run;
proc iml;
use time_series;
read all var{inflation};
close;
dif=dif(inflation);
lag=lag(dif,1:5);
want=inflation||dif||lag;
vname={'inflation'}||('dinf_lag0':'dinf_lag5');
create want from want[c=vname];
append from want;
close;
quit;
proc print data=want;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1698233276147.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89073i168813AF88D89DA6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1698233276147.png" alt="Ksharp_0-1698233276147.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 11:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899926#M83044</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-10-25T11:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899955#M83046</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;Thank You so much for your suggestion.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 13:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop/m-p/899955#M83046</guid>
      <dc:creator>Golf</dc:creator>
      <dc:date>2023-10-25T13:09:35Z</dc:date>
    </item>
  </channel>
</rss>

