BookmarkSubscribeRSS Feed
Anandkvn
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
Anandkvn
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 @Anandkvn 

 

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;

@Anandkvn 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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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