BookmarkSubscribeRSS Feed
ZhihanZhou
Calcite | Level 5

Hello everyone,

i want to find maximum values within a range and base on the maximum value creat a new column. Here as an example and my code, 

 

idzwecklead
14284
1654
170
15-13
11167
1256
170
22201
2187
270

 

data Hz.y;
set Hz.WegeketteP3lead;
by lead;
retain max_val;
drop max_val;
if lead ne 0 then max_val=lead;
else do;
max_val=max(maxval,lead);
maxLead=max_val;
end;

if max_val then do zweckH=catx('-',zweck,7);
end;
run;

 

what i want is to find the maximum value between the number before 0 and the maximum value stay just in the same row as orignal like following, at the meantime a new created column 'zweckH' will get the 'zweck'  from the row of 'mal_val' and plus7 till 'zweck' is 7:

idzweckleadmax_valzweckH
142842844-7
165404-7
17004-7
15-1301-7
111671671-7
125601-7
17001-7
222012012-7
218702-7
27002-7
4 REPLIES 4
mkeintz
PROC Star

Read each group twice (where "group" means every record until either the last of the id, or until lead=0).  In the first group-read, determine the max lead value, and the corresponding zweck. 

 

Prior to the second read establish zweckh.

 

Then, in the second pass, reread and test each observation to see lead matches the max value, and adjust the max_val variable accordingly ...and output the observations.

data want (drop=_:);
  do until (last.id=1 or lead=0);
    set have;
    by id;
    _max=max(_max,lead);
    length zweckh $8;
    if lead=_max then zweckh=cats(zweck);
  end;
  zweckh=catx('-',zweckh,zweck);

  do until (last.id=1 or lead=0);
    set have;
    by id;
    if lead<_max then max_val=0;
    else max_val=_max;
    output;
  end;
run;

 

Please provide your sample data in the form of a working data step if you want responses with tested code.  Respondents did that task for you in your previous request, but now it would be worth your time to do it yourself.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ZhihanZhou
Calcite | Level 5
Hello mkeintz,

It works great and simple to unterstand. In addition, I have a question about a condition in this sentence ‘ do until (last.id=1 or lead=0);’, the loop will run till the end of the table which lead by id, so here ‘last.Id=1’ means the value of last.id is 1 or last.if on the first row? Because I’ve tried both situations change or without change the number ‘1’, the result I got are the same and right.
CurtisMackWSIPP
Lapis Lazuli | Level 10

Since mkeintz hasn't answered you follow-up question I'll try to answer.

 

The last.id syntax is returns a 1 if this is the last row with the current value of id. It returns 0 otherwise. This goes along with the "by id" statement that states the data is sorted by the id variable. So the loop runs until this is the last row with a given value for id, or lead=0.

ZhihanZhou
Calcite | Level 5
Hello CurtisMackWSIPP,

Thanks for your great answer!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1020 views
  • 0 likes
  • 3 in conversation