SAS:保持数组中的非缺失值

时间:2016-08-29 14:47:53

标签: arrays sas do-loops

我有一个date1-date99的数据集,每次观察最多有5个非缺失日期。我想只保留非缺失日期并将它们分配给datenew1-datenew5。有关最佳方法的任何建议。我想在另一个do循环中进行循环(i = 1到5和j = 1到99),但是我不记得第一个非缺失时移动到下一个值的最佳方法已找到日期。

1 个答案:

答案 0 :(得分:0)

我认为这样做:

data have;
informat total_nonmissing;
do j = 1 to 1000; /*Number of rows to output*/
    array date[99];
    chosen = 0;
    remaining = dim(date);
    do i = 1 to remaining;
        call missing(date[i]);
        if ranuni(2) < (5-chosen) / remaining then do;
            date[i] = round(ranuni(1)*10000);
            chosen + 1;
            remaining + (-1);               
        end;
    end;
    total_nonmissing = n(of date{*});
    format date: yymmdd10.;
    output;
    drop i j chosen remaining;
end;
run;

data want;
    set have;
    array newdate[5];
    array date[99];
    j = 1;
    do i = 1 to dim(date);
        if not(missing(date[i])) then do;
            newdate[j] = date[i];
            j + 1;
        end;
    end;
    format newdate: yymmdd10.;
    keep newdate:;
run;