[英]sas looping through the dataset make the value missing depending on first row
我有一個與此類似的數據集。我不確定我會得到多少行或多少行,因為它是代碼的一部分。 但我將有第一個值等於 0 桶。
DATA MY_data;
INPUT bucket D_201503 D_201504 ;
DATALINES;
0 1000 20500
1 200 6700
2 101 456
3 45 567
;
例如 - 在這個數據集中,我希望應該缺少低於第一行值 10% 的值。 例如,對於桶 0,第一個值是 1000,因此應該缺少 45。 20500 也是如此。低於 10% 的任何東西都應該缺失。 數據集一般不大,但需要確定列和行。 所以我應該得到這個
0 1000 20500
1 200 6700
2 101 .
3 . .
我不確定我應該如何遍歷數據集並使這個條件
DATA MY_data;
INPUT bucket D_201503 D_201504 ;
DATALINES;
0 1000 20500
1 200 6700
2 101 456
3 45 567
;
data want;
set MY_data;
array row(*) _all_;
array _first_row(999); /*any number >= the number of columns of MY_data*/
/*we read the first line and store the values in _first_row array*/
retain _first_row:;
if _n_ = 1 then do i=1 to dim(row);
_first_row(i) = row(i);
end;
/*replacing values <10% of the first row*/
else do i=1 to dim(row);
if upcase(vname(row(i))) ne "BUCKET" and row(i) < 0.1*_first_row(i) then row(i) = .;
end;
drop i _first_row:;
run;
/*Find out how many variables there are (assume we just want all vars prefixed D_)*/
data _null_;
set my_data(obs = 1);
array vars_of_interest(*) D_:;
call symput(dim(vars_of_interest),"nvars")
run;
/*Save bucket 0 values to a temp array, compare each row and set missing values*/
data want;
set my_data;
array bucket_0(&nvars) _temporary_;
array vars_of_interest(*) D_:;
do i = 1 to &nvars;
if bucket = 0 then bucket_0[i] = vars_of_interest[i];
else if vars_of_interest[i] < bucket_0[i] / 100 then call missing(vname(vars_of_interest[i]))
end;
run;
您需要一種方法來記住第一行(或者可能來自 BUCKET=0 的行?)的值,以便您可以將第一行的值與當前值進行比較。 臨時 ARRAY 是一種簡單的方法。
因此,假設 BUCKET 始終是數據中的第一個數字變量,那么您就可以執行以下操作。
data want ;
set my_data;
array x _numeric_;
array y (1000) _temporary_;
do i=2 to dim(x);
if bucket=0 then y(i)=x(i);
else if x(i) < y(i) then x(i)=.;
end;
drop i;
run;
如果 BUCKET 不是第一個變量,那么您可以添加retain bucket;
在set
語句之前強制它成為第一個。 或者更改第一個數組語句以列出要處理的特定變量,只需記住更改DO循環上的下限即可。
如果您有超過一千個變量,則增加臨時數組的維數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.