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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

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

PG
Reeza
Super User

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;


michtka
Fluorite | Level 6

Just Brilliant, Thank you.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1201 views
  • 3 likes
  • 3 in conversation