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
What do you mean by "name of age"?
And is your question how to re-engineer the sql code in a data step?
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;
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...
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,
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
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!
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.