I would greatly appreciate if someone helps me with the code to count the number of id_job
combinations for the merged dataset below: I have a total of 17 obs with 8 id_jobs: os1-1,os1-1 and os1-1
is 1 id-job combination;os1-2,os1-2,os1-2,os1-2 and os1-2 is another id-job combination,etc
for a total of 8 id-job combinations. For cla_expo & bio-exp, 0=unexposed and 1=exposed.
My task is to count the number of id-job exposed for cla and bio: For cla(idchem=99005),bio(idchem=990021):
For cla: number of id_job exposed is 5/8=62.5%, bio:3/8=37.5%
Please find attached the output for cla, bio and merged cla and bio.
I tried proc freq idchem(not shown) but it didn't give me the right percentages.
Could someone help me with the SAS code to compute the percent id_job exposed for cla and bio,please
Also for the id exposed : For cla(idchem=990005), % id exposed is 3/4 = 75%
Thanks very much.
data idnew1;
input id$ job idchem;
datalines;
os1 1 990005
os1 1 9900021
os1 1 211700
os1 2 211700
os1 2 9900021
os1 2 210701
os1 2 990005
os2 1 210701
os2 1 990005
os2 2 9900021
os2 3 210701
os2 3 990005
os3 3 210701
os3 1 211700
os4 1 210701
os4 1 990005
os4 1 211700
;
run;
Hi @ak2011 ,
You can try this. The idea is to retrieve separately frequencies and totals, and then to calculate percentages.
In my opinion, it is not necessary to create additional variables (CLA_EXP, ... )
Hope this help!
Best,
proc sql;
select a.idchem, a.frequency / b.total as percentage format=Percent8.2
from (select idchem, count(id_job) as frequency from m1 group by idchem) as a,
(select count(distinct(id_job)) as total from m1) as b;
quit;
Hi @ak2011 ,
You can try this. The idea is to retrieve separately frequencies and totals, and then to calculate percentages.
In my opinion, it is not necessary to create additional variables (CLA_EXP, ... )
Hope this help!
Best,
proc sql;
select a.idchem, a.frequency / b.total as percentage format=Percent8.2
from (select idchem, count(id_job) as frequency from m1 group by idchem) as a,
(select count(distinct(id_job)) as total from m1) as b;
quit;
Hi @ak2011 ,
you're welcome
!
Hi @ak2011 ,
I have added the numerator (variable "frequency") and the denominator (variable "total") in the select clause:
data m1;
set idnew1;
id_job = catx("_",id,job);
run;
proc sql;
select a.idchem,
a.frequency,
b.total,
a.frequency / b.total as percentage format=Percent8.2
from (select idchem, count(id_job) as frequency from m1 group by idchem) as a,
(select count(distinct(id_job)) as total from m1) as b;
quit;
The result is the following :
If you want to have a table rather than a report, you can add the clause "create table <dataset name> as" before the select clause in the same statement.
Hi @ak2011
You can add the "order by" clause just before the "quit" statement as follows:
data m1;
set idnew1;
id_job = catx("_",id,job);
run;
proc sql;
select a.idchem,
a.frequency,
b.total,
a.frequency / b.total as percentage format=Percent8.2
from (select idchem, count(id_job) as frequency from m1 group by idchem) as a,
(select count(distinct(id_job)) as total from m1) as b
order by a.frequency desc;
quit;
Best,
You're welcome!
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.