BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
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 

SexAgeage_wisecount
F111
F122
F132
F142
F152
M111
M123
M131
M142
M152
M161
9 REPLIES 9
BrahmanandaRao
Lapis Lazuli | Level 10

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

BrahmanandaRao
Lapis Lazuli | Level 10
any one can solve this
mklangley
Lapis Lazuli | Level 10

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

 

mklangley_0-1602257534729.png

 

ballardw
Super User

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.

BrahmanandaRao
Lapis Lazuli | Level 10
Yes equalent output with datastep
ballardw
Super User

@BrahmanandaRao wrote:
Yes equalent output with datastep

Why?

 

What have you tried so far?

BrahmanandaRao
Lapis Lazuli | Level 10
we cannot do solve with datastep
Reeza
Super User

@ballardw  from previous threads it appears these are interview questions.

 

@BrahmanandaRao I've merged your two questions into one as they appear identical.

Ksharp
Super User
/* 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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 1211 views
  • 0 likes
  • 5 in conversation