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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

6 REPLIES 6
MarkWik
Quartz | Level 8

Can you please show your attempt using proc sql

novinosrin
Tourmaline | Level 20

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;

Reeza
Super User

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

novinosrin
Tourmaline | Level 20

@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.

Reeza
Super User

@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...

ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1585 views
  • 3 likes
  • 5 in conversation