Hi All,
I want to find the maximum value of observation from a variable using data step only.
we can not use any procedures and END option for data step.
Example :
EmpID EmpSal EmpName
E001 5000 A
E002 7500 B
E003 2500 C
E004 9800 D
E005 6100 E
I am looking for the maximum salaried EmpID / EmpName or whatever which should contain maximum value from a variable.
Regards
Uma Shanker
No procedures at all, and no END= on the DATA step:
data want;
set have nobs=_nobs_;
if EmpSal > max_EmpSal then do;
max_EmpSal = EmpSal;
max_EmpID = EmpID;
max_EmpName = EmpName;
end;
retain max_:;
keep max_:;
if _n_ = _nobs_;
run;
Why in the world do you have these restrictions?
What's up with the phony restrictions on PROC's? Is this a part of your training?
To solve this in the data step, use RETAIN.
proc sort data =emp;by descending EmpSal ;run;
data emp_1;
set emp;
cnt +1;
if cnt = 1;
run;
No procedures at all, and no END= on the DATA step:
data want;
set have nobs=_nobs_;
if EmpSal > max_EmpSal then do;
max_EmpSal = EmpSal;
max_EmpID = EmpID;
max_EmpName = EmpName;
end;
retain max_:;
keep max_:;
if _n_ = _nobs_;
run;
Why in the world do you have these restrictions?
Hi,
Thanks to all for your quick response, specially to "Astounding".
It's not all about the training and restrictions. It's only to explore the new ways to finalize the required output but yes it is something to learn in a limited bowl of resource and knowledge or may be sometime with restrictions.....
I am highly thankful for all answers...
Regards
Uma Shanker Saini
Here is another approach using Hash in data step, and it is one of a few times that Hash is not as twice verbose as conventional data step.
data _null_;
if 0 then set have;
declare hash h(dataset:'have', ordered:'d' , multidata:'y');
h.definekey('EmpSal');
h.definedata(all:'y');
h.definedone();
h.output(dataset:'want(obs=1)');
run;
Haikuo
Hi Hai.Kuo / Ksharp,
I am not much familiar with Hash.
would any one love to provide me good stuff ( online path or pdf ) so that i can learn.
email : umashankersaini@yahoo.com
Regards
Uma Shanker Saini
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.