BookmarkSubscribeRSS Feed
chd1425
Calcite | Level 5

Hi, 

I would like to retain the AETOXGR variable as AETOXGR_base untill AETOXGR is greater than the previous AETOXGR_base. The problem is that the AETOXGR_base should not change even if the previous AETOXGR was higher, but not higher than another previous AETOXGR. The data should be sorted by SubjectKey and startdate. 

 

Example: 

DATA have: 

__SUBJECTKEYSTARTDATEAETOXGR
101/Jan/231
102/Jan/232
103/Jan/231
104/Jan/233
105/Jan/231
106/Jan/232

DATA want:

__SUBJECTKEYSTARTDATEAETOXGRAETOXGR_base
101/Jan/2311
102/Jan/2322
103/Jan/2312
104/Jan/2333
105/Jan/2313
106/Jan/2323
3 REPLIES 3
yabwon
Onyx | Level 15
retain AETOXGR_base .;

AETOXGR_base = max(AETOXGR_base, AETOXGR);
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Use a RETAIN statement.

data want;
  set have;
  by subjectkey startdate;
  retain aetoxgr_max ;
  if first.subjectkey then aetoxgr_max =.;
  aetoxgr_max =max(aetoxgr_max,aetoxgr);
run;
ballardw
Super User

Isn't it amazing that when the question is about retaining a value that SAS has a Retain statement to address that need? 👍

 

Perhaps a note that this is not available in Proc SQL is appropriate just in case someone tries it with that approach.

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
  • 3 replies
  • 454 views
  • 1 like
  • 4 in conversation