BookmarkSubscribeRSS Feed
sydtbro
Calcite | Level 5

Hi, I have a dataset, and I need to create frequency tables to compare seatbelt use that represents those who wear them always or most of the time compared to those who wear them sometimes, rarely, or never.  I have to compare the seatbelt frequency by Sex, Grade, Race/Ethnicity, and Frequency of driving with drunk drivers in the past month. 

 

I know I have to recode the variables and use if/then statements, but I am not sure how to word this in SAS, 

2 REPLIES 2
ballardw
Super User

What exactly does your current data look like?

 

If you have values in one variable that need to be used in different ways you can create and assign a format to do groupings without changing the data.

Consider if I want to know the pre-teens or teens by sex in a data set. SASHELP.CLASS has a data set you can use such as:

proc freq data=sashelp.class;
   tables sex*age;
run;

With a custom format I can turn age into age categories:

proc format;
value teengrp
0-12 = 'Pre-teen'
13-17= 'Teen'
18-high = 'Adult'
;
run;

proc freq data=sashelp.class;
   tables sex*age;
   format age teengrp.;
run;

Proc format creates a format named teengrp that assigns numeric variable age in years to 3 groups as shown. Then associate the format using the format statement in the procedure.

You can use lists of values separated by commas to assign to one formatted value as well as numeric ranges.

Character formats are named starting with $ and the values are in quotes.

 

The concept of using formats for grouping values of a variable is a very powerful tool in SAS as you do not need to write bunches of if/then/else code and keep adding variables to a data set. In my work I have about a dozen age related formats alone base on need: 3-year, 5-year and 10-year age groups, over/under some cutoff value for qualification in a program, several similar to the preteen example for different reporting groups in different program uses.

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 507 views
  • 0 likes
  • 3 in conversation