0

How do I apply an if statement to a large number of variables without having to rewrite the condition. For example, suppose I have a set of variables sex, age, height ... (about 60 variables) and I want to specify the condition

if sex = . then sex = -99; if age = . then age = -99; . . . All 60 variables that are present in the data set. Is there a quick way to do this?

Thanks

3 Answers3

2

Use an array.

array arr{60} sex age height .... ;

do i = 1 to 60;
  if arr{i} = . then arr{i} = -99;
end;

That said, do consider whether recoding missing values like this is really what you want to do. Most SAS procedures know about missing values and can handle them in a reasonable way; turning them into a numeric value can bite you in the behind. For example, if you try to calculate the sum or mean using PROC SUMMARY, your result will no longer make sense; similarly if you try to analyse your data using a statistical procedure.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • 1
    `array arr _numeric_;` seems right if you want to do all of them (as the poster seems to). – Joe May 17 '13 at 19:31
1

In addition to the answer by Hongi Ooi, if you want to do this for all of the variables in your dataset (it sounds like you do) and don't want to type them out, you can get SAS to provide you with the list of variables to feed into your array.

Example:

%macro getvars(dsn);
%global vlist;
proc sql;
    select name into :vlist separated by ' '
    from dictionary.columns
    where memname=upcase("&dsn");
quit;
%mend;

Here's a reference for this and other examples: How to read variable names in a SAS data set?

Also, you may want to consider using the missing function if you want this to work for string variables too.

E.G. if missing(var) then do;

Community
  • 1
  • 1
ESmyth5988
  • 518
  • 2
  • 9
  • 20
  • It would be a good idea to include in your answer the relevant part of the code from the link here; while including the link for detail and/or to go beyond the answer's scope, answers in SO should generally be self-complete without referencing other websites. (http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers for more information.) – Joe May 17 '13 at 19:34
0
data have;
input x y z a b;
datalines;
1 2 3 . 5
4 5 . 1 2
4 3 . . 1
;;;;
run;

proc stdize data=have out=want reponly missing=-99; 
run;

If you really want to do all of them (or list those you want to do). STDIZE is part of SAS/STAT so hopefully you have that.

Joe
  • 62,789
  • 6
  • 49
  • 67