BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 10
proc sql;
create table new as select distinct 
patientid
,(case when group = "-1" then 1 else 0 end) as group_ind
from lib1.file2
;
quit;


proc sql;

create table want  as

select 


        count(distinct patientid) as total,

    count(distinct group_ind)>0))as id_ct_r,

       
calculated id_ct_r / total as percent_group format percent7.2
from new;
quit;

i need to calculate the percentage. I can do with datastep but would like to try proc sql. Above one has an error message. Can anyone give advice or provide a better way to calculate the rate, a percentage, how many with the value "-1" for the variable group (it is a character variable, and the value "-1" has a special meaning. All other values would be a eight-number value.

Thank you.

3 REPLIES 3
art297
Opal | Level 21

Show example have and want data sets in the form of a datastep. Your first call to proc sql reduces your data to one record for each patientid recoded group combination .. which may not be what you want to start with.

 

Art, CEO, AnalystFinder.com

HB
Barite | Level 11 HB
Barite | Level 11

You would think SQL like

 

select

sum(field1) as sum1,

count(field1) as count1,

sum1/count1 as myaverage

 

from

someplace

 

would work, but it never does. 

 

You always end up repeating the expressions and not being able to use the aliases. Which is annoying.

 

select

sum(field1) as sum1,

count(field1) as count1,

sum(field1)/count(field1) as myaverage

 

from wherever

 

 

Yes, I know there is an AVG function.

Tom
Super User Tom
Super User

It always helps to show some example data.

So if you had this data:

 

data have ;
  input group $ patientid $ @@;
cards;
1 1 1 2 1 3 
-1 1 -1 2
2 4 2 5
;

 

 

But it sounds you want something like

select count(distinct patientid) as N_patients
     , count(distinct case when group='-1' then patientid end) as N_minus_one
     , calculated N_minus_one / calculated N_patients as Percent_minus_one
  from have
;

Then you would get 5,2 and 0.4 as the answers since there are 5 distinct PATIENTID values of which only 2 are in the '-1' group.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 3 replies
  • 32418 views
  • 0 likes
  • 4 in conversation