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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 829 views
  • 1 like
  • 4 in conversation