DATA Step, Macro, Functions and more

finding percentage dataset

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

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

 

Capture.PNG

 

I want output like below.

 

Capture.PNG


Accepted Solutions
Solution
‎09-03-2017 05:27 AM
PROC Star
Posts: 283

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;

View solution in original post


All Replies
Frequent Contributor
Posts: 75

Re: finding percentage dataset

Posted in reply to shivamarrora0

Can you please show your attempt using proc sql

Solution
‎09-03-2017 05:27 AM
PROC Star
Posts: 283

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: 19,822

Re: finding percentage dataset

Posted in reply to shivamarrora0

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. 

 

What about PROC TABULATE? 

 

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: 283

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: 19,822

Re: finding percentage dataset

Posted in reply to novinosrin

@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: 11,343

Re: finding percentage dataset

Posted in reply to shivamarrora0

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 200 views
  • 3 likes
  • 5 in conversation