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.
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;
Can you please show your attempt using proc sql
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;
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
@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.
@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...
@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.
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!
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.