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

How can I generate below Flag variable that it restarts at 0 once the new maturity date is set?

Account ID As_of_Date Maturity_Date Flag
1A 4/30/2022 8/1/2022 0
1A 5/31/2022 8/1/2022 0
1A 6/30/2022 8/1/2022 0
1A 7/31/2022 8/1/2022 1
1A 8/31/2022 9/1/2022 0
1A 9/30/2022 9/1/2022 1
1A 10/31/2022 12/1/2022 0
1A 11/30/2022 12/1/2022 1
1A 12/31/2022 1/1/2023 0
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

As to the question in your message body:

Looks like you just want to save the value of the temporary LAST. automatic flag that is created when you use BY statement.

data want;
 set have;
  by accountID Maturity_Date;
  flag=last.Maturity_Date;
run;

As to the question in your Subject line:

Normally when you are creating a new variable in a data step.

x2 = x*x ;

SAS will reset that variable to missing when it starts the next iteration of the data step.  What the RETAIN statement does it let you mark the variables that you don't want SAS to reset to missing.  So at the start of the next iteration the variable has the value it had at the end of the previous iteration.  That is why it is so useful for remembering things or creating accumulators.

data want;
  set have;
   by id ;
   retain running_total;
    if first.id then running_total=0;
    running_total=sum(running_total,amount);
run;

SAS even has a special statement, the sum statement, to make doing accumulators even easier.

data want;
  set have;
   by id ;
   if first.id then running_total=0;
   running_total + amount;
run;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Not sure why you are asking about RETAIN, as this doesn't seem to be needed in the problem you talk about.

 

data want;
    set have;
    by maturity_date;
    if last.maturity_date then flag=1; 
    else flag=0;
run;
--
Paige Miller
tampham92
Obsidian | Level 7

I don't have that Flag variable yet. I want to generate that Flag variable as shown in the dataset.

My original dataset only 3 variables: Account ID, As of Date and Maturity Date.

 

Thanks.

PaigeMiller
Diamond | Level 26

Does the code I provided work for you?

 

Best advice: don't specify that you need to use a specific part of SAS (in this case RETAIN), just ask the question and let the responders pick a method that works.

--
Paige Miller
tampham92
Obsidian | Level 7

The code does not work. I tried and it starts dropping some of my observations off the dataset.

The original dataset contains multiple Account IDs sorted by Account ID and As of Date.

Thanks for the advice btw.

PaigeMiller
Diamond | Level 26

@tampham92 wrote:

The code does not work. I tried and it starts dropping some of my observations off the dataset.

The original dataset contains multiple Account IDs sorted by Account ID and As of Date.

Thanks for the advice btw.


Nothing in my code will cause observations to be deleted. Can you show us the exact code you used, and the output data set? (I see @Tom has provided almost the same code)

--
Paige Miller
tampham92
Obsidian | Level 7

I think when I just sort the data by "maturity date" without accountID, somehow it drops observations.

It works now once I add accountID back to "BY" statement. Thanks a lot for your and @Tom's help.

PaigeMiller
Diamond | Level 26

@tampham92 

 

Tip for next time: if you want code that works over multiple IDs, give example data that has multiple IDs, and state you want code that works on multiple IDs in your original message.

--
Paige Miller
Tom
Super User Tom
Super User

As to the question in your message body:

Looks like you just want to save the value of the temporary LAST. automatic flag that is created when you use BY statement.

data want;
 set have;
  by accountID Maturity_Date;
  flag=last.Maturity_Date;
run;

As to the question in your Subject line:

Normally when you are creating a new variable in a data step.

x2 = x*x ;

SAS will reset that variable to missing when it starts the next iteration of the data step.  What the RETAIN statement does it let you mark the variables that you don't want SAS to reset to missing.  So at the start of the next iteration the variable has the value it had at the end of the previous iteration.  That is why it is so useful for remembering things or creating accumulators.

data want;
  set have;
   by id ;
   retain running_total;
    if first.id then running_total=0;
    running_total=sum(running_total,amount);
run;

SAS even has a special statement, the sum statement, to make doing accumulators even easier.

data want;
  set have;
   by id ;
   if first.id then running_total=0;
   running_total + amount;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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