如何在sas中选择特定值

时间:2014-10-09 19:58:59

标签: sql sas

假设我有这样的数据集:

value1    value2   value3
X1       X2       X3
Y1       Y2       Y3
Z1       Z2       Z3

我想选择Y1,Z1和X2为三个新变量。

像这样:

a1 = Y1
a2 = Z1
a3 = X2

在回答Reese时,这就是我想要你的想法

%macro coeff(i=, j=, k=);
data want_&k.;
set new;
array va(3) value1 value2 value3;

%if &i.=1 %then %do;
        va(_n_)=va(_n_+&j.);
%end;

%else %if &i.=2 %then %do;
        va(_n_)=va(_n_+&j.);
%end;

keep value1;
run;
%mend;

%coeff(i=1, j=1, k=1);
%coeff(i=1, j=2, k=2);
%coeff(i=2, j=1, k=3);
%coeff(i=2, j=1, k=4);
%coeff(i=3, j=-2, k=5);
%coeff(i=3, j=-1, k=6);

但它不起作用。你能看看吗?

2 个答案:

答案 0 :(得分:0)

这回答了你的第一个问题,但我感觉不是你真正追求的。 我还在第二个数据集has2中添加了,如果数据在该结构中,也许你可以更好地解释你所追求的内容。

data have;
input value1 $    value2 $  value3 $;
cards;
X1       X2       X3
Y1       Y2       Y3
Z1       Z2       Z3
;
run;

data want;
set have;
array va(3) value1-value3;

if _n_=1 then va(_n_)=va(_n_+1);

keep value1;
run;


data have2;
    set have;
    array va(3) value1-value3;

    do i=1 to 3;
        value=va(i);
        row=_n_;
        output;
    end;
    keep value row;
run;

答案 1 :(得分:0)

这对我来说完全没有意义,上下文会有所帮助。

data have;
input value1 $    value2 $  value3 $;
cards;
X1       X2       X3
Y1       Y2       Y3
Z1       Z2       Z3
;
run;

data have2;
    set have end=eof;
    array va(3) value1-value3;
    retain counter 2;
    do i=1 to 3;
        value=va(i);
        row=_n_;
        if eof and i=3 then counter=1;
        output;
        counter+1;
    end;
    keep value counter;
run;

proc sort data=have2;
by counter;
run;

data have3;
    set have2;
    retain counter2 0;
    if mod(counter,3)=1 then counter2+1;
    drop counter i row;
run;

data _null_;
    set have3;
    by counter2;
    name=cats("a",counter2);
    if first.counter2 then do;
        call execute ("data " || name || "; set have3; where counter2=" ||counter2|| ";run;");
    end;
run;