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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 942 views
  • 3 likes
  • 3 in conversation