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.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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 531 views
  • 1 like
  • 2 in conversation