Hi @Ksharp, thanks for confirmation and sorry about keeping asking you this question. I have two requirements for the past three months data: 1. replace the current month std as missing if there are less than 5 observations in the past three months 2. replace the current month std as missing if there is any month with all missing data in the past three months If in the data, for one permno, there is data for past three months, but each month only have one observation, then this case does not fit the requirement of at least 5 observations (requirement 1) but fit the requirement of nonmissing data for any month among the past three months data (requirement 2). In order to take care of this extreme case, can I add both count and n(of x{*})>3 ? By the way, does n(of x{*})>3 ? mean at least three observations or at least three months of data ? Since the requirement 2 requires at least three months of data instead of at least 3 observations Is my code correct if combining both requirements as follows ? Thanks again! count=0; /************/
do until(last.PERMNO);
set have;
by PERMNO;
n=0;call missing(of x{*});
do i=intnx('month',monyy,-3) to intnx('month',monyy,-1,'e');
rc=h.find(key:i);
do while(rc=0);
if not missing(_RET) then count+1; /*************/
n+1;x{n}=_ret;
rc=h.find_next(key:i);
end;
end; if n(of x{*})>3 then do; std=std(of x{*}); /* <-- Change it into your Cross STD*/ if count<5 then std=.; /***********
... View more