proc sql;
select sex,age ,count(age) as age_wisecount from sashelp.class
group by sex ,age;
quit;
Here I am trying below output datasetp
Sex | Age | age_wisecount |
F | 11 | 1 |
F | 12 | 2 |
F | 13 | 2 |
F | 14 | 2 |
F | 15 | 2 |
M | 11 | 1 |
M | 12 | 3 |
M | 13 | 1 |
M | 14 | 2 |
M | 15 | 2 |
M | 16 | 1 |
How to find how many people are there for age
11,12,13,14,15,16 for Sex M and F
using data step and proc sql
(It looks like this is posted twice: https://communities.sas.com/t5/SAS-Programming/Count-Age-group-Gender-Wise/m-p/690371 )
@BrahmanandaRao Is this what you're looking for?
data have;
input sex $ age;
datalines;
F 11
F 12
F 12
F 13
F 14
F 14
F 14
F 15
F 16
F 16
M 11
M 12
M 12
M 13
M 13
M 14
M 15
M 15
M 16
;
run;
proc sql;
create table want_proc_sql as
select sex, age, count(*) as age_count
from have
group by sex, age;
quit;
/* Note: HAVE is assumed to be sorted by SEX then AGE */
data want_data_step;
set have;
by sex age;
retain age_count 0;
if first.age then age_count = 1;
if age = lag(age) then age_count + 1;
if last.age then output;
run;
Result for both WANT_PROC_SQL and WANT_DATA_STEP:
Since the code you show generates the shown output your question is not clear.
Are you asking how to get equivalent output with data step coding? If so, why?
Other methods such as Proc Means or Freq would be more appropriate.
@ballardw from previous threads it appears these are interview questions.
@BrahmanandaRao I've merged your two questions into one as they appear identical.
/* Do you want Hash Table solution ? */
data have; set sashelp.class; keep sex age; run; proc sort data=have ; by sex age; run; data want; do count=1 by 1 until(last.age); set have; by sex age; end; run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.