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


 

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

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1657 views
  • 1 like
  • 4 in conversation