BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

The following proc sql will allow to have a zero frequency for females aged 16.

 

proc sql;
    create table cnt as   
        select distinct sex, age, count(*) as cnt
        from sashelp.class
        group by sex, age;
    
    create table ref as 
        select distinct a.sex, b.age, 0 as cnt
        from sashelp.class a, sashelp.class b;
    
    create table sex_age_cnt as
        select a.sex, a.age, coalesce(b.cnt,a.cnt) as cnt
        from ref a
        left join 
             cnt b
        on a.sex=b.sex and
           a.age=b.age;
quit;

proc_sql_zero_frequency.JPGproc_sql_zero_frequency.JPG

I can also get the result using proc means.

 

proc means data=sashelp.class completetypes;
    class sex age;
run;

Is there any smarter way to get the same result using proc sql/proc freq?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

PROC FREQ with the SPARSE option

proc freq data=sashelp.class;
tables sex*age / sparse list out=want;
run;

is shorter than PROC SQL:

proc sql;
create table want2 as
select sex, age, count(a.sex) as cnt from 
(select distinct b.sex, c.age from sashelp.class b, sashelp.class c)
natural left join
sashelp.class a
group by 1,2;
quit;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

PROC FREQ with the SPARSE option

proc freq data=sashelp.class;
tables sex*age / sparse list out=want;
run;

is shorter than PROC SQL:

proc sql;
create table want2 as
select sex, age, count(a.sex) as cnt from 
(select distinct b.sex, c.age from sashelp.class b, sashelp.class c)
natural left join
sashelp.class a
group by 1,2;
quit;
xxformat_com
Barite | Level 11

Thanks. I forgot about the *.

For those reading this post, notice that with ods output, the sparse option is not needed.

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2148 views
  • 1 like
  • 2 in conversation