Solved
Contributor
Posts: 45

# finding percentage dataset

Hi All,

I want to find the percentage of sex by male and female  using datastep.. i can find using proc freq or proc sql but can one help me with base sas

I want output like below.

Accepted Solutions
Solution
‎09-03-2017 05:27 AM
PROC Star
Posts: 1,836

## Re: finding percentage dataset

Yep like @MarkWik mentioned, it would be interesting to know how you did by proc sql.

Is this how you did it?

proc sql;

select a.sex,count, count/c*100 as pct

from (select sex,count(sex) as count from sashelp.class group by sex) a,(select count(sex) as c from sashelp.class) ;

quit;

/*datastep*/

proc sort data=sashelp.class out=have;

by sex;

run;

data want;

set have nobs=nobs;

by sex;

if first.sex then count=0;

count+1;

if last.sex then do;

pct=count/nobs*100;

output;

end;

run;

All Replies
Frequent Contributor
Posts: 105

## Re: finding percentage dataset

Solution
‎09-03-2017 05:27 AM
PROC Star
Posts: 1,836

## Re: finding percentage dataset

Yep like @MarkWik mentioned, it would be interesting to know how you did by proc sql.

Is this how you did it?

proc sql;

select a.sex,count, count/c*100 as pct

from (select sex,count(sex) as count from sashelp.class group by sex) a,(select count(sex) as c from sashelp.class) ;

quit;

/*datastep*/

proc sort data=sashelp.class out=have;

by sex;

run;

data want;

set have nobs=nobs;

by sex;

if first.sex then count=0;

count+1;

if last.sex then do;

pct=count/nobs*100;

output;

end;

run;

Super User
Posts: 23,774

## Re: finding percentage dataset

So you want the PROC FREQ output but not using PROC FREQ?

DoW loops in a data step are the most efficient method for this, but I highly suspect this is homework so you should probably use a method that's in your class notes.

If this isn't homework and you're trying to find the most efficient method, here's a DoW loop tutorial

http://support.sas.com/resources/papers/proceedings12/052-2012.pdf

PROC Star
Posts: 1,836

## Re: finding percentage dataset

@Reeza You are right. However, questions of this kind makes me feel OP isn't completely sound with one pass of PDV construct. It may be little too intimidating to jump to DOW until one is thorough with traditional step by step process. One needs a little more time I think. Well, just my 2 cents.

Super User
Posts: 23,774

## Re: finding percentage dataset

@novinosrin I fully agree, but it would depend on if it was genuinely homework or not. Someone learning for general knowledge may want to go down that route, in comparison a students goal is to answer homework. Although I highly suspect it's homework I don't want to dissuade someone who is trying to learn.  Theoretically they should overlap but students don't usually have the time...

Super User
Posts: 13,583

## Re: finding percentage dataset

shivamarrora0 wrote:

Hi All,

I want to find the percentage of sex by male and female  using datastep.. i can find using proc freq or proc sql but can one help me with base sas

Note that Proc SQL and Proc Freq are "base SAS".

Datastep is for low level data manipulation most often to create input into Proc Freq or other analysis or reporting procedure.

☑ This topic is solved.