BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

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;

5 REPLIES 5
Tom
Super User Tom
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

FreelanceReinh
Jade | Level 19

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.
Reeza
Super User

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;

ybz12003
Rhodochrosite | Level 12

Thanks for all your help!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 7468 views
  • 7 likes
  • 5 in conversation