Hi Madam/Sir,
I want to construct the standard deviation of quarterly sales over the the preceding 16 quarters using the attached file ("songdata").
GVKEY | DATADATE | FYEARQ | FQTR | saleq |
1004 | 19850228 | 1984 | 3 | 50.939 |
1004 | 19850531 | 1984 | 4 | 60.128 |
1004 | 19850831 | 1985 | 1 | 53.836 |
1004 | 19851130 | 1985 | 2 | 61.04 |
1004 | 19860228 | 1985 | 3 | 64.541 |
1004 | 19860531 | 1985 | 4 | 68.595 |
1004 | 19860831 | 1986 | 1 | 66.376 |
1004 | 19861130 | 1986 | 2 | 75.907 |
1004 | 19870228 | 1986 | 3 | 71.83 |
1004 | 19870531 | 1986 | 4 | 84.079 |
1004 | 19870831 | 1987 | 1 | 74.543 |
1004 | 19871130 | 1987 | 2 | 81.31 |
1004 | 19880229 | 1987 | 3 | 92.951 |
1004 | 19880531 | 1987 | 4 | 100.858 |
1004 | 19880831 | 1988 | 1 | 90.007 |
1004 | 19881130 | 1988 | 2 | 100.645 |
I try to find the relevant code, but it is hard to revise this code. Any help will be highly appreciated.
data a2; set a1;
by gvkey;
array t {16} _temporary_;
if first.gvkey then call missing(of t{*});
do fy=sum(lag(fyearq),1) to fyearq-1;
t{1+mod(fy,16)}=.;
stdsale=std(of t{*});
run;
Thank you
Joon1
data want;
array p{0:15} _temporary_;
set have;
by gvkey;
if first.gvkey then call missing(of p{*});
p{mod(_n_,16)} = saleq;
stdsales = std( of p(*));
run;
For this approach to work you must have continuous time periods. If your time periods are not continuous and you're missing quarters this approach will not work.
https://gist.github.com/statgeek/27e23c015eae7953eff2
data want;
array p{0:15} _temporary_;
set have;
by gvkey;
if first.gvkey then call missing(of p{*});
p{mod(_n_,16)} = saleq;
stdsales = std( of p(*));
run;
For this approach to work you must have continuous time periods. If your time periods are not continuous and you're missing quarters this approach will not work.
https://gist.github.com/statgeek/27e23c015eae7953eff2
Thank you so much. I greatly appreciate for your help.
Joon1
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.