BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

thanikondharish_0-1615718822050.png 

I have sashelp.class data set like above dataset and how to do group wise analysis in datastep block?

first output:

how to sex wise sum and insert extra record group wise in dataset(we should do data step block only)

for ref:

thanikondharish_1-1615719081081.png

 

 

6 REPLIES 6
ballardw
Super User

Why?

Including such summary data in a data set means that you can't use it for almost any purpose except printing. And Proc Report will do that.

or Proc Summary and looking at the proper _type_ values.

 

proc summary data=sashelp.class;
   class sex name;
   var age height weight;
   types name*sex sex;
   output out=work.summary (drop=_freq_) sum=;
run;

proc sort data=work.summary 
    out=work.want (drop=_type_);
   by sex descending _type_ name;
run;

 

Ksharp
Super User
data have;
set sashelp.class;
run;

proc report data=have nowd out=x;
column name sex _sex  age height weight;
define name/display;
define sex/order noprint;
define _sex/computed 'sex';
define age/analysis sum;
define height/analysis sum;
define weight/analysis sum;
compute before sex;
 temp=sex;
endcomp;
compute _sex/character length=20;
 _sex=temp;
endcomp;
compute after sex;
_sex=cats(sex,'-total');
endcomp;
break after sex/summarize;
run;
Ksharp
Super User
proc sort data=sashelp.class out=have;
by sex;
run;

data want;
length name sex $ 20;
call missing(_age,_height,_weight);
 do until(last.sex);
   set have;
   by sex;
   _age+age;_weight+weight;_height+height;
   output;
 end;
call missing(name,age,weight,height);
sex=cats(sex,'-total');
age=_age;weight=_weight;height=_height;
output;
drop _:;
run;
mkeintz
PROC Star

Let SAS sequence your data via the power of where:

 

data want (drop=_:);
  set sashelp.class (where=(sex='F'))
      sashelp.class (where=(sex='M'));
  by sex;
  _age+age;
  _height+height;
  _weight+weight;
  output;
  if last.sex;
  age=_age;
  height=_height;
  weight=_weight;
  name='Total';
  output;
  call missing(of _:);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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