From: greenwillow on
*First, I put the number of observations, and the variable's value of
freq to two series of macro variables.

proc sql noprint;
select count(*)
into :NObs
from test2;

select freq
into :freq1-:freq%left(&NObs)
from test2;
quit;


*Then I want to create a new variable called: sumlag which will sum up
the previous 25 observations of variable freq. However, the codes
following don't work. Would please help me out?

%macro lagcal;
data test3;
set test2;
%do j=1 %to &NObs;
sumlag=0; *if the j<25, the sumlag=0;
%if &j>=25 %then
%do i=&j-24 %to &j;
sumlag=sumlag+&&freq&i; *sumlag=the sum of previous 25
records of freq.
%end;
%end;
run;
%mend;

%lagcal;
From: Tom Abernathy on
On May 10, 10:26 am, greenwillow <yangliuy...(a)gmail.com> wrote:
> *First, I put the number of observations, and the variable's value of
> freq to two series of macro variables.
>
> proc sql noprint;
>   select count(*)
>     into :NObs
>   from test2;
>
>   select freq
>     into :freq1-:freq%left(&NObs)
>   from test2;
> quit;
>
> *Then I want to create a new variable called: sumlag which will sum up
> the previous 25 observations of variable freq. However, the codes
> following don't work. Would please help me out?
>
> %macro lagcal;
> data test3;
> set test2;
> %do j=1 %to &NObs;
>   sumlag=0;                                *if the j<25, the sumlag=0;
>   %if &j>=25 %then
>   %do i=&j-24 %to &j;
>   sumlag=sumlag+&&freq&i;        *sumlag=the sum of previous 25
> records of freq.
>   %end;
> %end;
> run;
> %mend;
>
> %lagcal;

Not sure why you would consider using macro language for this.
Do you want something like this?
I used just the last 3 observations to make it easier to check by
hand.

data counts;
do i=1 to 10;
freq=int(100*ranuni(-1));
output;
end;
run;

data want;
set counts nobs=_nobs;
nobs=_nobs;
lag3 = lag3(freq);
if lag3=. then lag3=0;
cum3 +freq-lag3;
put i 3. freq 4. lag3 4. cum3 8. nobs 4.;
run;

1 27 0 27 10
2 70 0 97 10
3 8 0 105 10
4 88 27 166 10
5 5 70 101 10
6 85 8 178 10
7 54 88 144 10
8 56 5 195 10
9 94 85 204 10
10 54 54 204 10
From: greenwillow on
Thank you so much Tom. This is very helpful! It solves the
problem. :)

But I am still not sure the problem of the macro codes. If possible,
would you please help me to indentify the problems with the macro
codes?

Thanks,





On May 10, 10:23 am, Tom Abernathy <tom.aberna...(a)gmail.com> wrote:
> On May 10, 10:26 am, greenwillow <yangliuy...(a)gmail.com> wrote:
>
>
>
>
>
> > *First, I put the number of observations, and the variable's value of
> > freq to two series of macro variables.
>
> > proc sql noprint;
> >   select count(*)
> >     into :NObs
> >   from test2;
>
> >   select freq
> >     into :freq1-:freq%left(&NObs)
> >   from test2;
> > quit;
>
> > *Then I want to create a new variable called: sumlag which will sum up
> > the previous 25 observations of variable freq. However, the codes
> > following don't work. Would please help me out?
>
> > %macro lagcal;
> > data test3;
> > set test2;
> > %do j=1 %to &NObs;
> >   sumlag=0;                                *if the j<25, the sumlag=0;
> >   %if &j>=25 %then
> >   %do i=&j-24 %to &j;
> >   sumlag=sumlag+&&freq&i;        *sumlag=the sum of previous 25
> > records of freq.
> >   %end;
> > %end;
> > run;
> > %mend;
>
> > %lagcal;
>
> Not sure why you would consider using macro language for this.
> Do you want something like this?
> I used just the last 3 observations to make it easier to check by
> hand.
>
> data counts;
>   do i=1 to 10;
>     freq=int(100*ranuni(-1));
>     output;
>   end;
> run;
>
> data want;
>   set counts nobs=_nobs;
>   nobs=_nobs;
>   lag3 = lag3(freq);
>   if lag3=. then lag3=0;
>   cum3 +freq-lag3;
> put i 3. freq 4. lag3 4. cum3 8. nobs 4.;
> run;
>
>   1  27   0      27  10
>   2  70   0      97  10
>   3   8   0     105  10
>   4  88  27     166  10
>   5   5  70     101  10
>   6  85   8     178  10
>   7  54  88     144  10
>   8  56   5     195  10
>   9  94  85     204  10
>  10  54  54     204  10- Hide quoted text -
>
> - Show quoted text -