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

Hi All,

 

Would require assistance to achieve the following new amount value to be inputted based on a flag and the specified amount tied to it.

 

Please find screenshot for reference. For each account number, I would like to RETAIN the first amount at each occurrence of flag and affect subsequent records below. Once there's a new amount, it will take in the new amount and bring it down to the subsequent rows below. This behavior applies to the same account number until it reaches a new account number. 

 

sas_newbie94_0-1615479045716.png

data example;
input accno flag amount ;
datalines;
100 . .
100 . .
100 . .
100 . .
100 1 1000
100 1 .
100 1 .
100 1 .
100 1 3000
100 1 .

200 . .
200 . .
200 . .
200 . .
200 . .
200 1 500
200 1 .
200 1 .
200 1 700
200 1 .
;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way might be:

data want; 
   set example;
   by accno;
   retain new_amount;
   if first.accno then new_amount=.;
   if flag and amount then new_amount=amount;
   if missing(flag) then new_amount=.;
run;

This works for the provided example.

The " if flag and amount then new_amount=amount;" works if flag and amount are not 0 as SAS treats non-zero non-missing numeric values as "true". If zero is a possible valid value for either then use

if not missing(flag) and not missing(amount) then new_amount=amount;

You did not show if the Flag value might ever be missing after it was set within an Accno group. I am guessing that you likely would want to reset the new_amount to missing if that every occurs.

View solution in original post

1 REPLY 1
ballardw
Super User

One way might be:

data want; 
   set example;
   by accno;
   retain new_amount;
   if first.accno then new_amount=.;
   if flag and amount then new_amount=amount;
   if missing(flag) then new_amount=.;
run;

This works for the provided example.

The " if flag and amount then new_amount=amount;" works if flag and amount are not 0 as SAS treats non-zero non-missing numeric values as "true". If zero is a possible valid value for either then use

if not missing(flag) and not missing(amount) then new_amount=amount;

You did not show if the Flag value might ever be missing after it was set within an Accno group. I am guessing that you likely would want to reset the new_amount to missing if that every occurs.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1 reply
  • 841 views
  • 0 likes
  • 2 in conversation