Hi,
My code below look back 3 row and count number of value positive.
It works quite fine except for the last rows of where the value is maxed at 4.
I try to do the "do n=_n_ to _n_+3 UNTIL eof" but fail.
Can you please help to fix it?
Thanks,
HHC
data h;
input Year_Month State $ Value;
datalines;
201401 CA -1
201402 CA .
201403 CA 2
201404 CA 5
201405 CA 8
201405 CA 2
201401 TX 1
201402 TX -4
201403 TX .
201404 TX 4
201405 TX 3
201405 TX 1
;
data w;
keep Year_Month state value c ;
set h nobs=totalobs;
c=0;
if value>0 then do;
do n =_n_ to _n_+3;
set h(rename =(Year_Month =ym state=st value=vl )) point=n;
if st=state and vl>0 then c=c+1;
end;
end;
run;
Your code appears to be looking ahead, not back.
You want to look at the current and three following observations?
do pointer=_n_ to min(_n_+3,totalobs);
set h(....) point=pointer;
...
end;
If you want to look back you could use LAG() function. Or you could modify your DO loop.
do pointer=_n_ to max(_n_-3,1) by -1;
set h(....) point=pointer;
...
end;
Your code appears to be looking ahead, not back.
You want to look at the current and three following observations?
do pointer=_n_ to min(_n_+3,totalobs);
set h(....) point=pointer;
...
end;
If you want to look back you could use LAG() function. Or you could modify your DO loop.
do pointer=_n_ to max(_n_-3,1) by -1;
set h(....) point=pointer;
...
end;
Hi Tom,
Thanks for your comment.
I actually didn't know how to do look back and I got to do Sort before lol.
HHC
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.