Hi all! I've a question about a variable when it is not applicable. In my dataset there is a variable for diabetes. Persons with diabetes get a 1 and persons without get a 0. There is also a variable which contains the age of diabetes diagnosis. So only for the diabetes=1 persons there is an age in the diabetes diagnosis variable. The others with a diabetes=0, I give a '999' as in not applicable in the diabetes age variable. The real missings get a dot. But now for calculating the mean age of diabetes diagnosis, SAS takes also the 999 into account. What could I do to take only the 'real' diabetes ages into account in the mean statement and not the 999 values? So that it is treated in the same way as the real missing values with a dot? I want to do this in the MEAN statement and NOT working with IF diabetes=1 THEN .. as it is possible.
Thanks!
Instead of using 999 to indicate not applicable, you would be better off using a special missing value. For example:
if var = 999 then var = .N ;
That way, you can distinguish .N from . and yet PROC MEANS knows enough to throw both values out of the calculations.
What does your code look like now?
A simple where statement may solve your problem.
Instead of using 999 to indicate not applicable, you would be better off using a special missing value. For example:
if var = 999 then var = .N ;
That way, you can distinguish .N from . and yet PROC MEANS knows enough to throw both values out of the calculations.
Okay, I tried this. But then the values of .N are also counted as missing values in PROC MEANS NMISS statement. What can I do to let PROC MEANS know that this is not a 'missing value' and that it also not is taken into acount in the calculations?
PROC MEANS can't do that. A value is either missing (and excluded from calculations) or not missing (and included in calculations). There are no other choices as far as PROC MEANS is concerned.
If you want separate counts, you could use PROC FREQ. For example:
proc format;
value valid low-high='Valid';
run;
proc freq data=have;
tables var / missing;
format var valid.;
run;
Be sure to include the MISSING option to get separate counts for each type of missing value.
If the 999 gets excluded entirely from numerator and denominator then use a WHERE.
Where not missing(var);
Hi,
Another way by using proc sql:
proc sql;
select diabetes,mean(age) as mean_age
from have
group by diabetes;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.