Quartz | Level 8

## Loop

Hi everyone,
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?

data use;
set use;

dinf = dif(inflation); /* Gerate first different of inflation */
array dinf_lag[5] ; /* Create an array of 5 variables */
do i = 1 to 5;
%let i;
%put dinf_lag[&i] = lag&i(dinf); /* Generating variable names dinf_lag1,...,dinf_lag5 */
end;
drop i; /* Drop the loop counter variable from the final dataset */
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Loop

``````/*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;
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;``````

15 REPLIES 15
Opal | Level 21

## Re: Loop

The macro statements will execute before your data step and though won't be affected in any way by your do loop.

Please provide some sample have data via working SAS datastep code and then show us the desired result for this sample data.

Quartz | Level 8

## Re: Loop

Patrick,

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)?

This is the desired data.

Opal | Level 21

## Re: Loop

Please provide some sample have data via working SAS datastep code

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.

Super User

## Re: Loop

Your code is equivalent to this:

``````%let i;
%put dinf_lag[&i] = lag&i(dinf);

data use;
set use;
dinf = dif(inflation);
array dinf_lag[5];
do i = 1 to 5;
end;
drop i;
run;``````

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.

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.

``````%macro loop;
%do i = 1 %to 5;
dinf_lag[&i] = lag&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;``````
Quartz | Level 8

## Re: Loop

Hi Kurt_Bremser,

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?

Super User

## Re: Loop

@Golf wrote:

Hi Kurt_Bremser,

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?

Please supply example data in a working DATA step with DATALINES, and show the expected result.

Quartz | Level 8

## Re: Loop

Here is my desired data for lead variables.

I use the simple codes as follows:

proc expand data= ch6dols.time_series out=ch6dols.time_series;

convert dinf;
run;

I would like to know how to generate this dataset in a loop. It would be helpful to generate longer leads and lags.

Thank You.

Super User

## Re: Loop

Please supply example data in a working DATA step with DATALINES.

We need to see with what you start initially (the original ch6dols.time_series, before conversion).

No pictures, code text which we can copy to our program windows.

Quartz | Level 8

## Re: Loop

Here is the codes:

PROC IMPORT DATAFILE="/home/u45083887/my_content/SM518/SM518 Ch5/time_series_ex1.xlsx"
OUT=Ch6DOLS.time_series
DBMS=XLSX
REPLACE;
RUN;

data ch6dols.time_series;
set ch6dols.time_series;
dinf = dif(inflation);

proc expand data= ch6dols.time_series out=ch6dols.time_series;

convert dinf;
run;

proc print data = ch6dols.time_series;
run;

Thank You.

PROC Star

## Re: Loop

While having a cup of coffee, i repeat what as been asked twice already:

Please supply example data in a working DATA step with DATALINES.

We don't have access to your Excel file and many of us won't open excel files attached to posts.

Quartz | Level 8

## Re: Loop

Here it is.
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;

Thanks
Super User

## Re: Loop

When I combine your data with the picture of the wanted data, I think I see this:

• you create, for every observation, the difference to the previous year
• then, you look ahead for this difference 1 to 5 years

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.

Quartz | Level 8

Super User

## Re: Loop

``````/*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;
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;``````

Discussion stats
• 15 replies
• 675 views
• 5 likes
• 5 in conversation