0
votes

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
Use an array and a for loop.Blazemonger
SAS does not have for, it is a do loop ;)Joe
Also, I was very disappointed to open this thread and not find a reason to write an actually recursive macro or FCMP routine...Joe

3 Answers

2
votes

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.

1
votes

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;

0
votes
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.