Hi guys, anyone can help me to simplify my code in the way to format my final dataset want?...maybe using loops rather than write down by hand the 4 combinations (class/type)?
data new;
input name class $ type $;
datalines;
1 a yes
2 a no
3 a yes
4 b no
5 b yes
6 a yes
7 b no
8 b no
9 b yes
10 a yes
11 a yes
;
run;
*creating macrovariables;
proc sql noprint;
select count(distinct name) into: safea
from new
where class='a';
quit;
proc sql noprint;
select count(distinct name) into: safeb
from new
where class='b';
quit;
%put &safea;
%put &safeb;
proc sql noprint;
create table test as
select distinct class, type, count(distinct name) as n
from new
group by class,type;
quit;
*creating the format of my want dataset;
data want;
set test;
if class='a' and type='yes' then nc=put(n,best12.)||'('||put(n/&safea*100,5.1)||')';
if class='a' and type='no' then nc=put(n,best12.)||'('||put(n/&safea*100,5.1)||')';
if class='b' and type='yes' then nc=put(n,best12.)||'('||put(n/&safeb*100,5.1)||')';
if class='b' and type='no' then nc=put(n,best12.)||'('||put(n/&safeb*100,5.1)||')';
drop n;
run;
Thanks
V.
You could do this more compactly as folows :
proc sql;
create table want as
select
class,
type,
catt(ncCount, " (", put(100*ncCount/sum(ncCount), 5.1), ")") as nc
from (select class, type, count(*) as ncCount from new group by class, type)
group by class;
quit;
PG
You could do this more compactly as folows :
proc sql;
create table want as
select
class,
type,
catt(ncCount, " (", put(100*ncCount/sum(ncCount), 5.1), ")") as nc
from (select class, type, count(*) as ncCount from new group by class, type)
group by class;
quit;
PG
I prefer using procedures, such as proc freq.
proc sort data=new; by class type;
proc freq data=new noprint;
by class;
table type/out=want1 totpct;
run;
data want2;
set want1;
nc=cat("", trim(strip(put(count, 8.))), "(",trim(strip(put(percent/100, percent8.1))), ")");
run;
Just Brilliant, Thank you.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.