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

Hello,

 

Say that I have a table like this:

 Measure1Measure2Measure3
Id1426
Id2739
Id381

5

 

And I want Proc SQL to output the following table:

 CountPercentage
Id11226.7
Id21942.2
Id314

31.1

 

Count is a row's sum and percentage is a row's sum*100/column sum of "Count". How can I go about doing this? Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input id $ measure1-measure3;
cards;
Id1	4	2	6
Id2	7	3	9
Id3	8	1	5
;

proc sql;
create table want as
select id,sum(measure1,measure2,measure3) as count, calculated count/sum( calculated count)*100 as pct
from have;
quit;

View solution in original post

9 REPLIES 9
Reeza
Super User
Why the requirement to do it with SQL?
novinosrin
Tourmaline | Level 20

data have;
input id $ measure1-measure3;
cards;
Id1	4	2	6
Id2	7	3	9
Id3	8	1	5
;

proc sql;
create table want as
select id,sum(measure1,measure2,measure3) as count, calculated count/sum( calculated count)*100 as pct
from have;
quit;
novinosrin
Tourmaline | Level 20

or instead of *100, apply format percent.

 


data have;
input id $ measure1-measure3;
cards;
Id1	4	2	6
Id2	7	3	9
Id3	8	1	5
;

proc sql;
create table want as
select id,sum(measure1,measure2,measure3) as count, calculated count/sum( calculated count)  as pct format=percent.
from have;
quit;
learsaas
Quartz | Level 8
proc sql noprint;
	create table result as
		select (case when Measure1=. then 0 else Measure1 end)+
		       (case when Measure2=. then 0 else Measure2 end)+
			   (case when Measure3=. then 0 else Measure3 end) as count,
			   (case when calculated count in (.,0) then . else round(calculated count/sum(calculated count)*100,.1)  end) as Percentage
		from a;
quit;
Reeza
Super User

@learsaas wrote:
proc sql noprint;
	create table result as
		select (case when Measure1=. then 0 else Measure1 end)+
		       (case when Measure2=. then 0 else Measure2 end)+
			   (case when Measure3=. then 0 else Measure3 end) as count,
			   (case when calculated count in (.,0) then . else round(calculated count/sum(calculated count)*100,.1)  end) as Percentage
		from a;
quit;

The SUM() function accounts for missing by essentially considering it as 0. 

novinosrin
Tourmaline | Level 20

Are you so wide awake at this time? Kudos . I wish I had your energy. 

learsaas
Quartz | Level 8

@Reeza wrote:

@learsaas wrote:
proc sql noprint;
	create table result as
		select (case when Measure1=. then 0 else Measure1 end)+
		       (case when Measure2=. then 0 else Measure2 end)+
			   (case when Measure3=. then 0 else Measure3 end) as count,
			   (case when calculated count in (.,0) then . else round(calculated count/sum(calculated count)*100,.1)  end) as Percentage
		from a;
quit;

The SUM() function accounts for missing by essentially considering it as 0. 

 

 

The SUM() function accounts for missing by essentially considering it as 0. ?????????????????????

data _null_;
	x=sum(.);
	put x=;
run;

 


 

andreas_lds
Jade | Level 19

Are there always exactly three measure-variables?

 

If not, i would start by transposing the in source dataset to get rid of the not-that-perfect structure:

 

proc transpose data= work.have out=work.transposed(rename=(_name_=MeasureNo col1=value));
   by id;
   var measure1 measure2 measure3;
run;

 

Then you can get rid of proc sql  and use a reporting procedure like proc tabluate to generate the required output:

proc tabulate data=work.transposed;
   var value;
   class id / order=unformatted missing;
   table
      id,
      value*(sum colpctsum)
   ;
run;

 

 

torvyle
Calcite | Level 5

Thanks everyone. There are more than 3 "measure" variables so novinosrin's solution seems to be the most efficient.

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 54867 views
  • 2 likes
  • 5 in conversation