BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.


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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
LinusH
Tourmaline | Level 20

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
TarunKumar
Pyrite | Level 9

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

data emp_1;

set emp;

cnt +1;

if cnt = 1;

run;

Astounding
PROC Star

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?

umashankersaini
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

umashankersaini
Quartz | Level 8

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 6 replies
  • 6410 views
  • 5 likes
  • 5 in conversation