BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BenBrady
Obsidian | Level 7

I have made a variable called age_group which has been dervied from an existing variable age. the variable age_group is split up as follows:

 

age_group=1 if age<35

age_group=2 if  35<=age<=75

age_group=3 if age>75.

 

When I run a proc freq on the data, age_group=3 does not appear in the output table because there are 0 observations of this in the data. I am wandering how I can get the proc freq to display a frequency of 0 for age_group=3.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why do you need to use proc freq?  Proc freq provides counts of data which is present.  In most scenarios where I have to do this, then I merge the results back to the distinct list of options:

proc sql;
  create table WANT as
  select  COALESCE(A.AGE_GROUP,B.AGE_GROUP) as AGE_GROUP,
          case when B.N=. then 0 else B.N end as N
  from    (select distinct AGE_GROUP from HAVE) A
  full join MEANS_OUTPUT B
  on      A.AGE_GROUP=B.AGE_GROUP;
quit;

However, if you use the right procedure, proc means, then you can use nway (and also multi-label formats if you need to combinations and such like) and do it in one proc means step.

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

proc freq does not have an easy option for this, but proc means does. Do you need proc freq?

data CLASSES; 
  AGE=30; output; 
  AGE=60; output; 
  AGE=90; output; 
data HAVE; 
  AGE=60; output; 
  AGE=90; output; 
proc means data=HAVE classdata=CLASSES nway;
   class AGE;
   output out=FREQ;
run; 

 

AGE N Obs
30 0
60 1
90 1

 

BenBrady
Obsidian | Level 7
yes I do need to use proc freq
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why do you need to use proc freq?  Proc freq provides counts of data which is present.  In most scenarios where I have to do this, then I merge the results back to the distinct list of options:

proc sql;
  create table WANT as
  select  COALESCE(A.AGE_GROUP,B.AGE_GROUP) as AGE_GROUP,
          case when B.N=. then 0 else B.N end as N
  from    (select distinct AGE_GROUP from HAVE) A
  full join MEANS_OUTPUT B
  on      A.AGE_GROUP=B.AGE_GROUP;
quit;

However, if you use the right procedure, proc means, then you can use nway (and also multi-label formats if you need to combinations and such like) and do it in one proc means step.

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
  • 4 replies
  • 3181 views
  • 0 likes
  • 4 in conversation