BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

Errors with proc freq

 

Current dataset

idsexethnicgroup
1malehispanicyes
2malenot hispanicyes
3femalehispanicyes
4femalehispanicno
5femalenot hispanicno

 

I want to get each variable (sex and ethnic) by treatment group

 

ods output onewayfreqs=c.demographics_freq;
proc freq data=c.demographics;
by group;
tables sex ethnic race ;
run;quit;
ods listing ;

 

but when i run it it looks like this:

grouptablesexfrequencypercentcumulative frequencycumulativeethnicethnic
notable sexfemale    hispanic 
notable sexmale    not hispanic 
yestable sexfemale    hispanic 
yestable sexmale    hispanicnot hispanic
yestable sexmale     not hispanic

 

I wanted more of as:

grouptable_LABEL_frequencypercentcumulative frequencycumulative
notable sexfemale    
notable sexmale    
yestable sexfemale    
yestable sexmale    
yestable ethnichispanic    
yestable ethnicnot hispanic    
notable ethnichispanic    
notable ethnicnot hispanic    

 

Help!

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

Hi @monday89 

 

Do you want to output a report or a dataset?

In your code, you mention the RACE variable. Or it is not in the entry dataset. Is it a mistake?

Thank you for the clarifications.

 

Based on the input data, you can try this:

data demographics;
	input id 1 sex $ 3-8 ethnic $ 10-21 group $ 23-25;
	datalines;
1 male   hispanic     yes
2 male   not hispanic yes
3 female hispanic     yes
4 female hispanic     no
5 female not hispanic no
;
run;

proc sort data=demographics;
	by group;
run;

ods output onewayfreqs=demographics_freq_sex;
	proc freq data=demographics;
		by group;
		tables sex;
	run;
ods listing;

ods output onewayfreqs=demographics_freq_ethnic;
	proc freq data=demographics;
		by group;
		tables ethnic;
	run;
ods listing;

data want;
	length _LABEL_ $20.;
	set demographics_freq_sex 	 (rename=(sex=_LABEL_) drop=F_sex)
		demographics_freq_ethnic (rename=(ethnic=_LABEL_) drop=F_ethnic);
run;

proc print data=want;
run;
monday89
Fluorite | Level 6

Yes, sorry it is mistake. No race to be reported. I prefer it to be a dataset. THANK YOU! 

PaigeMiller
Diamond | Level 26

PROC FREQ has only limited ability to produce custom data sets.

 

A little programming will get what you want. Here is an example using SASHELP.CLASS

 

proc freq data=sashelp.class;
tables sex age;
run;
data want;
    length category $ 8;
    set demographics_freq1;
    if strip(table)='Table Sex' then category=f_sex;
    else category=f_age;
    drop age sex f_age f_sex;
run;
        
--
Paige Miller
ed_sas_member
Meteorite | Level 14

Hi @monday89 

Thank you for the prompt answer Smiley Happy

 

I have updated my previous post with the code. Let me know!

Ksharp
Super User

You want this ?

 

data demographics;
	input id 1 sex $ 3-8 ethnic $ 10-21 group $ 23-25;
	datalines;
1 male   hispanic     yes
2 male   not hispanic yes
3 female hispanic     yes
4 female hispanic     no
5 female not hispanic no
;
run;
ods select none;
ods output  list=want;
proc freq data=demographics ;
table group*(sex ethnic)/list sparse;
run;
ods select all;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1579 views
  • 1 like
  • 4 in conversation