Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Calculation of rolling standard deviation using PROC EXPAND

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-12-2016 03:29 PM
(2153 views)

Dear All,

I have the following dataset:

data have; input id date X; datalines; 1 194503 . 1 194506 . 1 194509 0.1

2 194703 . 2 194706 0.2 2 194709 0.1

2 194712 0.3

2 194803 0.2;

I would like to compute the rolling standard deviation (STDX) for different IDs of the variable X using 3 observations (with no less than 2). For example, in row 5 after two observations of X (0.2 and 0.1), I want to compute STDX as 0.05. In row 6, after three observations of X (0.2, 0.1 and 0.3), I wnat to compute STDX as 0.082

data want; input id date X STDX; datalines; 1 194503 . . 1 194506 . . 1 194509 0.1 .

2 194703 . . 2 194706 0.2 . 2 194709 0.1 0.05

2 194712 0.3 0.082

2 194803 0.2 0.082;

My attempt involves proc expand as follows. Yet, I'm not able to get the desired result. Moreover, I'm also getting the following warning: *WARNING: The variable X has only 0 nonmissing observations, which is too few to apply** the conversion method. The result series is set to missing.*

Any help would be highly appreciated. Many thanks!

PROC EXPAND DATA=have OUT=want; by id; convert X=STDX / transformout=(MOVSTD 3 trim 2); RUN;

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Proc Expand is an undoubtedly powerful utility tool, however, because it is not a programming tool, it lacks certain level of flexibility that data step can conveniently offer.

```
data have;
input id date X;
datalines;
1 194503 .
1 194506 .
1 194509 0.1
2 194703 .
2 194706 0.2
2 194709 0.1
2 194712 0.3
2 194803 0.2
;
data want;
array _sd(0:2) _temporary_;
set have;
by id date;
if first.id then
do;
call missing(of _sd(*));
_n=0;
end;
_n+1;
_sd(mod(_n,3))=x;
if n(of _sd(*))>=2 then
stdx=std(of _sd(*));
drop _n;
run;
```

📢

**ANNOUNCEMENT**

The early bird rate has been extended! Register by March 18 for just $695 - $100 off the standard rate.

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.** **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.