The SAS Output Delivery System and reporting techniques

How to find max observation from a variable using data step only...

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 113
Accepted Solution

How to find max observation from a variable using data step only...


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


Accepted Solutions
Solution
‎06-11-2013 09:07 AM
Respected Advisor
Posts: 4,648

Re: How to find max observation from a variable using data step only...

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?

View solution in original post


All Replies
Esteemed Advisor
Posts: 5,087

Re: How to find max observation from a variable using data step only...

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.

Data never sleeps
Contributor
Posts: 68

Re: How to find max observation from a variable using data step only...

proc sort data =emp;by descending  EmpSal ;run;

data emp_1;

set emp;

cnt +1;

if cnt = 1;

run;

Solution
‎06-11-2013 09:07 AM
Respected Advisor
Posts: 4,648

Re: How to find max observation from a variable using data step only...

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?

Frequent Contributor
Posts: 113

Re: How to find max observation from a variable using data step only...

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

Respected Advisor
Posts: 3,107

Re: How to find max observation from a variable using data step only...

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

Frequent Contributor
Posts: 113

Re: How to find max observation from a variable using data step only...

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

Post a Question
Discussion Stats
  • 6 replies
  • 2619 views
  • 5 likes
  • 5 in conversation