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!
ARRAY
do u mind explainng a little more detail on how to write the code? im a sas novice.
supply example data.
Here's a good tutorial on Arrays
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=_:); 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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.