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

Is it possible to do this with proc freq?

 

data company;
	length co_name $50.;
 	input co_name $ 1-14 name $ 16-18 totalcontacts; 
 	datalines;
AAA Ford - BMC Dan 8
AAA Ford - BMC Jan 6
AAA Ford - BMC Mar 1
AAA Ford - BMC Tom 5
;
run;

proc freq noprint data=company;
	tables  name*totalcontacts/out=work.company2;
run;

 

I want the output to look like this:

AAA Ford - BMC Dan 40%

AAA Ford - BMC Jan 30%

AAA Ford - BMC Mar 5%

AAA Ford - BMC Tom 25%

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Yes you can do this with proc freq.  However proc freq produces a PERCENT variable that generates the number 20 to represent twenty percent, NOT the number 0.2.   The problem is that SAS has a percent. format that shows .2 as 20%, but has none to show 20 as 20%.

 

You can make your own format if you like:

 

proc format ;
  picture pct low-high='009%';
run;

proc freq noprint data=company;
	tables  co_name*name/out=work.company2 (drop=count);
	weight totalcontacts;
	format percent pct.;
run;
proc print data=company2;run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Very easy and straight forward in sql:

 

data company;
	length co_name $50.;
 	input co_name $ 1-14 name $ 16-18 totalcontacts; 
 	datalines;
AAA Ford - BMC Dan 8
AAA Ford - BMC Jan 6
AAA Ford - BMC Mar 1
AAA Ford - BMC Tom 5
;
run;
proc sql;
create table company2 as
select co_name,name, totalcontacts/sum(totalcontacts) as pct format=percent.
from company
group by  co_name;
quit;

 

dan999
Fluorite | Level 6

I know how to do it in SQL. I thought proc freq might be fewer steps and I'd learn something new. Thanks.

dan999
Fluorite | Level 6

So that's how you do it. Thanks for the help.

mkeintz
PROC Star

Yes you can do this with proc freq.  However proc freq produces a PERCENT variable that generates the number 20 to represent twenty percent, NOT the number 0.2.   The problem is that SAS has a percent. format that shows .2 as 20%, but has none to show 20 as 20%.

 

You can make your own format if you like:

 

proc format ;
  picture pct low-high='009%';
run;

proc freq noprint data=company;
	tables  co_name*name/out=work.company2 (drop=count);
	weight totalcontacts;
	format percent pct.;
run;
proc print data=company2;run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
dan999
Fluorite | Level 6

Sorry. Replied to wrong post. Thanks for the help.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1237 views
  • 0 likes
  • 3 in conversation