检查SAS列中的所有条目,然后返回虚拟变量SAS



我是SAS中的新手,想检查数据集中的变量中的所有条目是否满足条件(即= 1),并且仅返回一个虚拟变量0 pr 0 pr。变量中的条目是1或至少一个不是1。知道该怎么做吗?

IF colvar = 1 THEN dummy_variable = 1 

创建与原始变量相同大小的另一个变量dummy_variable。

谢谢

* Generate test data;
data have;
    colvar=0;
    colvar2=0;
    do i=1 to 20;
        colvar=round(ranuni(0));
        output;
    end;
    drop i;
run;
* Read the input dataset twice, first counting the number
* of observations and setting the dummy variables to 1 if
* the corresponding variable has the value 1 in any obser-
* vation, second outputting the result. The dummy variables
* remain unchanged during the second loop.;
data want;
    _n_=0;
    d_colvar=0;
    d_colvar2=0;
    do until (eof);
        set have end=eof;
        if colvar = 1
         then d_colvar=1;
        if colvar2 = 1
         then d_colvar2=1;
        * etc.... *;
        _n_=_n_+1;
    end;
    do _n_=1 to _n_;
        set have;
        output;
    end;
run;

proc SQL是快速生成任意定义条件的摘要的好工具。您的病情到底是什么不清楚的。我认为您希望表中的all_one值以下代码生成。当每个观察结果都有colvar = 1时,那将是1。任何不是一个值的值都会导致条件为false(0),因此all_one将其值为0而不是1。

您可以将结果存储在小表中。

proc sql ;
  create table check_if_one as
     select min( colvar=1 ) as all_one
          , max( colvar=1 ) as any_one
          , max( colvar ne 1 ) as any_not_one
          , min( colvar ne 1 ) as all_not_one
     from my_table
  ;
quit;

,但是您也可以将值存储到一个宏变量中

proc sql noprint ;
   select min( colvar=1 ) into :all_one trimmed from my_table ;
quit;

最新更新