tags:

views:

40

answers:

3

I have a variable Deck1-Deck100 (Deck1 through Deck100 representing 100 trials of a task) with four possible values A' B' C' D'. I need to recode this variable so that A' and B' = 0 and C' and D' = 1. Can you help? I know nothing about how to compress variables.

+1  A: 

Assuming that you have all 100 variables in one observation and just want to recode the values, the following might work. Just replace the number 4 with the number 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;

This gives you the following output:

Obs    deck1    deck2    deck3    deck4

 1      A'       B'       C'       D'  

Obs    deck1    deck2    deck3    deck4

 1       0        0        1        1  
Michael Hasan
+2  A: 

or the informat approach:

/* 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;

which produces this output:

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

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
rkoopmann
A: 

Striping the single quote is easy, just compress() it out:

data _null_;
  length old new $8.;
  old = "A'";
  new = compress(old, "'");
  put (_all_) (=/);
run;
/* on log
old=A'
new=A
*/
Chang Chung