making same changes to 900 variables

Reply
Frequent Contributor
Posts: 102

making same changes to 900 variables

hi have 900 variables in a dataset.

for each of them i want to do

data j2; set j1; If missing(var1)> 0 or var1 in ('NO', 'N', 'NA', ' ', '$$"ER", 9903') then newvar1= 0; Else newvar1= 1; run;

what is the b est way to do this in 900 variables?

thank you!

Respected Advisor
Posts: 3,799

Re: making same changes to 900 variables

ARRAY

Frequent Contributor
Posts: 102

Re: making same changes to 900 variables

Posted in reply to data_null__

do u mind explainng a little more detail on how to write the code? im a sas novice.

Respected Advisor
Posts: 3,799

Re: making same changes to 900 variables

supply example data.

Super User
Posts: 19,860

Re: making same changes to 900 variables

Here's a good tutorial on Arrays

Statistical Computing Seminar: Arrays in SAS

PROC Star
Posts: 7,490

Re: making same changes to 900 variables

If ALL of your variables are character variables, and NONE have a name that is longer than 31 characters, and you want to change ALL of them to be numeric variables according to your stated recode statement, and you want to keep the same variable names, then you could use a data _null_ step with arrays to write and run call execute statements to accomplish the task.

If all of the above is true, then the following code can accomplish the task:

/*create some example data*/

data j1;

  length this that something_else $12;

  input (this that something_else) (& $);

  cards;

1  y  n

na  n/a  5

x  $$"ER", 9903  no

;

/*create and run some call execute statements*/

data _null_;

  set j1 (obs=1);

  array all(*) _char_;

  length exline $200;

  call execute('data j2 (drop=_Smiley Happy; set j1 (rename=(');

  exline=catt(vname(all(1)),'=_',vname(all(1)));

  call execute(exline);

  do i=2 to dim(all);

    exline=cat(' ',strip(vname(all(i))),'=_',strip(vname(all(i))));

    call execute(exline);

  end;

  call execute('));');

  do i=1 to dim(all);

    exline=catt('if missing(_',vname(all(i)),') or upcase(_',

    vname(all(i)),

      ') in ("NO", "N", "NA", " ",',

      "'$$",'"ER", 9903',"') then do;",vname(all(i)),'=0;end;else do;',

      vname(all(i)),'= 1; end;');

    call execute(exline);

  end;

  call execute('run;');

run;

Ask a Question
Discussion stats
  • 5 replies
  • 252 views
  • 0 likes
  • 4 in conversation