Can you calculate the mean, median and mode of the following data set
using data step logic? Don't use any function or procedure.
data a;
input age @@;
datalines;
22 32 32 32 43 23 24 56 45 54 28 29 27 26 25 28 30 20 18 37 36 47 46
56 19 20
;
run;
I have calculated the mean which i have posted in the answer section.
Answer Posted / rg
data temp;
input age @@;
cards;
8 7 7 2 67 9 7 7 1 2 45 34 22 45 23 8 34 23
run;
proc sort data=temp; by age ;run;
data a(keep = median mode_age mean);
last_age=age;
retain max_cnt mode_age median;
set temp end=lastobs nobs=n;
by age;
age_temp + age;
/*Median */
if mod(n,2) = 1 and _n_ = (n+1)/2 then do ;median=age; end;
if mod(n,2) = 0 and _n_ = (n+2)/2 then do ;median=(age + last_age)/2; end;
/*Mean*/
if lastobs then do mean=age_temp/n ; end;
/*Mode */
if first.age then cnt =1; else cnt+1;
if last.age then lst_cnt=cnt;
if lst_cnt > max_cnt then do; max_cnt = lst_cnt; mode_age = age;end;
if lastobs then output;
run;
| Is This Answer Correct ? | 9 Yes | 1 No |
Post New Answer View All Answers
What are the statements in proc sql?
Explain how you can debug and test your SAS program?
Enlist the syntax rules followed in sas statements.
what are sas bi dashboard components? : Sas-bi
Do you need to compute new variables? If so,should you do this before you execute the report-writing procedure?
What is the basic structure of the SAS base program?
what are the new features included in the new version of sas i.e., Sas 9.1.3? : Sas programming
what is the difference between floor and ceil functions in sas? : Sas-administrator
In proc transpose and data step with arrays which one you pick?
how do you want missing values handled? : Sas programming
what is PhaseIII, ODS, TLG, Macro and Proc in SAS
what are the considerations when picking a SAS/STAT procedure?
Name some categories in sas 9? : sas-grid-administration
how do the in= variables improve the capability of a merge? : Sas programming
How long can a macro variable be? A token? : sas-macro