BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data dd;
set sashelp.class;
run;

proc sql;
	select age, count(age) from dd 
group by  age;
quit;

i want output in datastep group by age as well as name of age of agecount

5 REPLIES 5
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Krut,

I want same result like in datastep like proc sql

proc sql;
select age, count(age) as agecount from sashelp.class
group by age;
quit;

Kurt_Bremser
Super User

First, sort by age. Then use a data step with

by age;

retain the new variable agecount, set it to 1 at first.age, and increment it else. At last.age, output.

 

See examples for the use of by in a data step here: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=lestmtsref&docsetTarget=p... 

ed_sas_member
Meteorite | Level 14

Hi @BrahmanandaRao 

 

Here is a data step equivalent to your PROC SQL code:

/*PROC SQL */

proc sql;
	create table want as /* <-- add this code if you want to output a dataset */
	select age, count(age) as Age_N from sashelp.class 
	group by age;
quit;


/* DATA STEP EQUIVALENT */

proc sort data=sashelp.class out=class_sorted;
	by age;
run;

proc means data=class_sorted n nonobs;
	var age;
	class age;
	ways 1;
	output out=want (drop=_:) n= /autoname; /* <-- add this code if you want to output a dataset */
run;

All the best,

Reeza
Super User

This isn't a great idea for a data step either, the 'SAS' method to do this would be to use PROC FREQ (or REPORT, TABULATE, MEANS). This provides displayed output or a dataset. 

 

proc freq data=sashelp.class;
table age / out = want;
run;
proc means data=sashelp.class N;
class age;
output out=want_means N= ;
run;

@BrahmanandaRao wrote:
data dd;
set sashelp.class;
run;

proc sql;
	select age, count(age) from dd 
group by  age;
quit;

i want output in datastep group by age as well as name of age of agecount


 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1203 views
  • 1 like
  • 4 in conversation