Hello, all:
I kind of feel the flowing three tables generated by "proc freq" are too long. Is there a way to make the codes shorterned? Thanks.
Ying
proc freq data=A;
table Age * Smoke;
where Age in (1,2,3,4) and Smoke in (1,2,3);
format Age Age. SmokeStatus Smoke.;
run;
proc freq data=A;
table gender * SMOKE;
where gender in (1,2) and SMOKES in (1,2,3);
format gender gender. SMOKE SMOKE.;
run;
proc freq data=A;
table race * SMOKE;
where race in (1,2,3,4,5) and SMOKE in (1,2,3);
format race race. SMOKE SMOKE.;
run;
Is the output report too long or are you complaining about the SAS code?
The problem with attempting to shorten the code is that you are filtering the data differently in each of the runs. So the first table will allow records with out of range values for gender to still be summarized by age group. If you wanted to create the cross tabs all in one PROC call then you would either include all values of RACE, GENDER and AGE or limit your self to the subset of records that meet all of the restrictions.
But do you even need those WHERE clauses? If your AGE variable is coded as 1,2,3,4 or missing then PROC FREQ table will not include the missing values anyway without the need for the WHERE clause. Similarly do you even need to limit the values of SMOKE?
Does this produce the same results?
proc freq data=A;
table (Age Gender Race) * Smoke;
format
Age age.
Gender gender.
Race race.
Smoke smoke.
;
run;
If not then you might be able to use new formats to get the same effect.
proc format ;
value ageX 1,2,3,4=[age.] other=' ';
value genderX 1,2=[gender.] other=' ';
value raceX 1,2,3,4,5=[race.] other=' ';
value smokeX 1,2,3=[smoke.] other=' ';
run;
proc freq data=A;
table (Age Gender Race) * Smoke;
format
Age ageX.
Gender genderX.
Race raceX.
Smoke smokeX.
;
run;
I dont think so. The code is different for each proc, in the first you use smoke variable, in the other two it smokes. The where restriction is different for each. The format is different for each. You could perhaps save some time by doing:
data age_freq gender_freq race_freq;
set have;
format age age. gender gender. race race. ...;
select;
when (age in (1,2,3,4) and smoke in (1,2,3)) output age_freq;
when (gender in (1,2) and smokes in (1,2,3)) output gender_freq;
...;
end;
run;
data _null_;
do i="age","gender","race";
call execute(cats('proc freq; data=',i,'_freq; tables ',i,' * smoke'; run;'))
end;
run;
Hello,
I take it that you want to obtain a more concise output. There are at least to ways to accomplish this:
Try a proc tabulate. But as mentioned above you need to ensure that the source data is the same for all of the tables.
You can customize this in many different ways but hopefully it's a starting point.
proc tabulate data=have;
class age smoke gender race;
table age gender race, smoke*n;
run;
Thanks for all your help!
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.