DATA Step, Macro, Functions and more

Is there a way to combine the "proc freq" tables?

Reply
Super Contributor
Posts: 319

Is there a way to combine the "proc freq" tables?

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;

Super User
Super User
Posts: 7,039

Re: Is there a way to combine the "proc freq" tables?

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;
Super User
Super User
Posts: 7,942

Re: Is there a way to combine the "proc freq" tables?

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;

Trusted Advisor
Posts: 1,117

Re: Is there a way to combine the "proc freq" tables?

[ Edited ]

Hello,

 

I take it that you want to obtain a more concise output. There are at least to ways to accomplish this:

 

  • Use the LIST option of the TABLE(S) statement in order to obtain a one-dimensional table rather than a cross tabulation. Example: table Age * Smoke / list;
  • Use one or more of the three options NOROW, NOCOL or NOPERCENT in order to omit row, column or overall percentages, respectively, in the cross tabulation. Example: table Age * Smoke / nopercent norow nocol; This would restrict the output to the cell and total frequencies.
Super User
Posts: 19,770

Re: Is there a way to combine the "proc freq" tables?

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;

Super Contributor
Posts: 319

Re: Is there a way to combine the "proc freq" tables?

Thanks for all your help!

Ask a Question
Discussion stats
  • 5 replies
  • 518 views
  • 7 likes
  • 5 in conversation