1

我有一个变量 Deck1-Deck100(Deck1 到 Deck100 代表一个任务的 100 次试验),有四个可能的值 A' B' C' D'。我需要重新编码这个变量,使 A' 和 B' = 0 并且 C' 和 D' = 1。你能帮忙吗?我对如何压缩变量一无所知。

4

3 回答 3

2

或信息方法:

/* generate a fake data set */
data trials;
    array trial(100) $2.;
    array t(4) $2. _temporary_ ("A'","B'","C'","D'");
    do i = 1 to 100;
        j=ceil(ranuni(112233)*4);
        trial[i] = t[j];
    end;
    drop i j ;
run;
proc print noobs;
    var trial1-trial10;
run;

/* create a 'recoding' format */
proc format;
    invalue trials (upcase)
        "A'","B'"=0
        "C'","D'"=1
        other=.;
run;

/* convert the values */
data newtrials;
    set trials;
    array trial(*) $2. trial1-trial100;
    array rtrial(100);
    do i = 1 to 100;
        rtrial[i]=input(trial[i], trials.);
    end;
    drop i trial:;
run;
proc print noobs;
    var rtrial1-rtrial10;
run;

产生这个输出:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

trial1    trial2    trial3    trial4    trial5    trial6    trial7    trial8    trial9    trial10

  D'        D'        B'        A'        A'        D'        A'        B'        C'        D'

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

rtrial1   rtrial2   rtrial3   rtrial4   rtrial5   rtrial6   rtrial7   rtrial8   rtrial9   rtrial10

   1         1         0         0         0         1         0         0         1          1
于 2010-07-15T21:13:45.893 回答
1

假设您在一次观察中拥有所有 100 个变量,并且只想重新编码这些值,则以下方法可能有效。只需将数字 4 替换为数字 100。

data sample;

    deck1='A''';
    deck2='B''';
    deck3='C''';
    deck4='D''';
run;

proc print data=sample;
run;

data result;
    set sample;

    array alldecks(4) deck1-deck4;

    do i=1 to 4;
        if alldecks(i) eq 'A''' or alldecks(i) eq 'B''' then alldecks(i)='0';
        if alldecks(i) eq 'C''' or alldecks(i) eq 'D''' then alldecks(i)='1';
        end;

    drop i;
run;

proc print data=result;
run;

这将为您提供以下输出:

Obs    deck1    deck2    deck3    deck4

 1      A'       B'       C'       D'  

Obs    deck1    deck2    deck3    deck4

 1       0        0        1        1  
于 2010-07-15T19:12:35.033 回答
0

剥离单引号很容易,只需compress()

data _null_;
  length old new $8.;
  old = "A'";
  new = compress(old, "'");
  put (_all_) (=/);
run;
/* on log
old=A'
new=A
*/
于 2010-07-15T21:59:02.593 回答