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

Hello,

 

This is a Have data and I want to generate Flag variable as seen in Wanted?

 

Have:

Date Account ID Flag
4/30/2022 1 0
5/31/2022 1 0
6/30/2022 1 0
7/31/2022 1 1
8/31/2022 1 0
9/30/2022 1 0

 

Want:

Date Account ID Flag
4/30/2022 1 0
5/31/2022 1 0
6/30/2022 1 0
7/31/2022 1 1
8/31/2022 1 2
9/30/2022 1 3
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just use a SUM statement and the fact that SAS evaluates Boolean expressions to 1 for TRUE and 0 for FALSE.

 

data want;
  set have;
  by account date ;
  if first.account then new_flag=0;
  new_flag + (flag or new_flag);
run;

Result:

OBS    account          Date    Flag    new_flag

 1        1       2022-04-30      0         0
 2        1       2022-05-31      0         0
 3        1       2022-06-30      0         0
 4        1       2022-07-31      1         1
 5        1       2022-08-31      0         2
 6        1       2022-09-30      0         3

Note: You need to create a NEW variable for the RETAIN to work. If you try use a variable that is already on the input dataset then the retained value is lost when the next observation is read in.

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Just use a SUM statement and the fact that SAS evaluates Boolean expressions to 1 for TRUE and 0 for FALSE.

 

data want;
  set have;
  by account date ;
  if first.account then new_flag=0;
  new_flag + (flag or new_flag);
run;

Result:

OBS    account          Date    Flag    new_flag

 1        1       2022-04-30      0         0
 2        1       2022-05-31      0         0
 3        1       2022-06-30      0         0
 4        1       2022-07-31      1         1
 5        1       2022-08-31      0         2
 6        1       2022-09-30      0         3

Note: You need to create a NEW variable for the RETAIN to work. If you try use a variable that is already on the input dataset then the retained value is lost when the next observation is read in.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 209 views
  • 0 likes
  • 2 in conversation