How to find the maximum value in a variable through data steps without using loop, min and max fun.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to find the maximum value in a variable through data steps without using loop, min and max fun.

data have;
input Name $ Age;
cards;
Jack 10
Joe 31
Tom 22
Roe 33
Rex 44
Tim 24
;

 

How to find the maximum value of AGE variable through data steps without using loop, min and max function.


Accepted Solutions
Solution
‎01-15-2018 04:27 AM
Super User
Posts: 21,546

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

This is a commonly asked question when learning to program so it's within the realm of 'semi-decent' questions. It teaches you how information is carried across rows, conditional logic and how to identify the end of a file. There are automated ways, but often these basics are ignored. I suspect this is the answer your professor is looking for, since it's the most basic solution I can think of, besides the PROC SORT method.

 

Using the test data from @novinosrin, the approach outlined below works well. It matches his answer with the IFN so that's another viable option as well. 

 

Screen Shot 2018-01-09 at 5.35.08 PM.png

 

 

View solution in original post


All Replies
PROC Star
Posts: 831

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

You want to have "FUN" here? Hmm, I would like to learn from you. Do you know how to do it? If yes, please share. Thank you!

Occasional Contributor
Posts: 6

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to novinosrin

Actually, you interpret it wrong. Not using the inbuilt function max, min and loop.

Regular Contributor
Regular Contributor
Posts: 221

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

Why?

 

data have;
input Name $ Age;
cards;
Jack 10
Joe 31
Tom 22
Roe 33
Rex 44
Tim 24
;

proc sort;
	by descending age;
run;

data maxage;
	set have (obs= 1);
	drop name;
run;

 

The SAS System
 
Age
44
Super User
Posts: 12,148

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra
Respected Advisor
Posts: 2,174

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

Why avoid built-in procedures to do this? It makes no sense. It makes your life more difficult. A pointless restriction.

 

Unless it's a homework assignment, in which case you need to try it yourself, and ask questions when you get stuck. But it still makes no sense to do it this way.

--
Paige Miller
Super User
Posts: 21,546

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

[ Edited ]
Posted in reply to ajay_mishra

Does > and < logic count as max/min functions? Technically they're operators. And technically, the min/max function in a data step will not give you the min/max of a column without other logic.

 


ajay_mishra wrote:

data have;
input Name $ Age;
cards;
Jack 10
Joe 31
Tom 22
Roe 33
Rex 44
Tim 24
;

 

How to find the maximum value of AGE variable through data steps without using loop, min and max function.


 

 

Occasional Contributor
Posts: 6

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

No. It will act as an operator.

PROC Star
Posts: 831

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

[ Edited ]
Posted in reply to ajay_mishra


data test;
input Name $ Age;
cards;
Jack 10
Joe 31
Tom 22
Roe 33
Rex 44
Tim 24
;

proc transpose data=test out=w;
var age;
run;

 

data wantt;
set w;
array c(*) col:;
call sortn(of c: );
keep col6;
run;

 

data wantt2;
set test end=last;
array a(100) _temporary_;
retain a;
a(_n_)=age;
if last then do; call sortn(of a(*));max=a(dim(a));output;end;
keep max;
run;

PROC Star
Posts: 831

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to novinosrin

One more:

 


data wantt3;
set test end=last;
retain max;
max=ifn(Age>max,age, max);
if last;
keep max;
run;

Trusted Advisor
Posts: 1,149

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

I doubt if your class has gotten to hash objects, so here's a solution I'm sure is not contemplated by your instructor, but which satisfies the criteria.  I don't recommend submitting it:

 

data have;
input Name $ Age;
cards;
Jack 10
Joe 31
Tom 22
Roe 33
Rex 44
Tim 24
run;

data want;
  if 0 then set have;

  declare hash h (dataset:"have",ordered:'D');
    h.definekey('age');
    h.definedata(all:'Y');
    h.definedone();
  declare hiter ih ('h');

  ih.first();
run;
Solution
‎01-15-2018 04:27 AM
Super User
Posts: 21,546

Re: How to find the maximum value in a variable through data steps without using loop, min and max f

Posted in reply to ajay_mishra

This is a commonly asked question when learning to program so it's within the realm of 'semi-decent' questions. It teaches you how information is carried across rows, conditional logic and how to identify the end of a file. There are automated ways, but often these basics are ignored. I suspect this is the answer your professor is looking for, since it's the most basic solution I can think of, besides the PROC SORT method.

 

Using the test data from @novinosrin, the approach outlined below works well. It matches his answer with the IFN so that's another viable option as well. 

 

Screen Shot 2018-01-09 at 5.35.08 PM.png

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 244 views
  • 4 likes
  • 7 in conversation