vashts85
vashts85

Reputation: 1147

sas: recode a bunch of variables at once

Let's say I have 10 variables, all with the same prefix (they start with com).

Let's say the variables are binary, and there is some missing data, and I'd like to set all that missing data to 0. I'd like to generate a new variable for each original variable so the original data is intact.

How would I go about recoding all of these variables at once? Is there an easy way to run a loop in SAS? If this were Python or R I could write a loop to do it all using grep or something similar.

EDIT: Here's what I'm trying to do. I want to use an unindexed array with the com variables to create copies of them called new_com (prefix each variable with new). Then I just want to recode, which I know I can do using if then statements. The first part, about cloning the variables, is where I am stuck.

Upvotes: 0

Views: 871

Answers (2)

kstats9pt3
kstats9pt3

Reputation: 873

Below is edited in the event you do not know how many variables you have but know the prefix you want:

data test;
    input coma comet compton communism come comb community comerade complete comma;
    datalines;
    1   1   1   0   0   .   0   0   1   .
    ;
run;

%let prefix=com;

/* output the list of variables and only keep those with prefix */
proc contents data = test noprint out=names(keep=name varnum); run;

proc sort data = names;
    by varnum;
run;

/* create your new variable with a "new_" prefix */
data names1; set names;
    if index(name,"&prefix.");
    new_name = "new_"||strip(name);
run;

/* put lists into macro variables */
proc sql noprint;
    select name
    into: old_vars separated by " "
    from names1;

    select new_name
    into: new_vars separated by " "
    from names1;

    select count(*)
    into: nobs
    from names1;
quit;

%put &old_vars.;
%put &new_vars.;
%put &nobs.;

/* use array and macro variables to manipulate data */
data test1; set test;
    array old(&nobs.) &old_vars.;
    array new(&nobs.) &new_vars.;
        do i=1 to &nobs.;
            if old(i) = . then new(i) = 0;
                else new(i) = old(i);
        end;
    drop i;
run;

Upvotes: 1

Joe
Joe

Reputation: 63424

Assuming you have SAS/STAT licensed, this is most simply done using PROC STDIZE:

data have;
  array com[10];
  call streaminit(7);
  do _j = 1 to 10;
    do _i = 1 to 10;
      if rand('Uniform') < 0.2 the com[_i]=.;
      else com[_i]=1;
    end;
    output;
  end;
run;

proc stdize data=have out=have_zero missing=0 reponly sprefix=new_ oprefix=old_;
  var com:;
run;

reponly says to not do any standardization (only impute zero for missing), sprefix and oprefix specify what to prefix standardized and original variables with.

Upvotes: 1

Related Questions