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

I am trying to do a count of a condition by id  but for some reason my counts are not working properly. Need some advise. I have  data below:

 

Have

 

data have;
input @1 id @5 amount1 @7 amount2;
datalines;
345 70 20
345 30 30
345 80 50
412 30 14
412 70 20
412 20 90
412 30 80
563 40 30
563 50 60
;
run

 

I want if sum of amount1 and amount2  is greater or equal to 1 then flag+1 by ID

Want

ID

AMOUNT1

AMOUNT2

FLAG

345

70

20

.

345

30

30

.

345

80

50

1

412

30

14

.

412

70

20

.

412

20

90

1

412

30

80

2

563

40

30

.

563

50

60

1

563

40

100

2

563

50

70

3

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @kashun,

 


@kashun wrote:

I want if sum of amount1 and amount2  is greater or equal to 1 then flag+1 by ID

I assume you mean " ... to 100 ..."

data want;
set have;
by id;
if first.id then flag=.;
if amount1+amount2>=100 then flag+1;
run;

It's not clear from your example what should happen to the flag if the sum drops below 100 within a BY group. The code above retains the previous value in this case. If you want FLAG to be missing instead, use a second flag variable like this:

data want(drop=_:);
set have;
by id;
if first.id then _flag=.;
if amount1+amount2>=100 then do;
  _flag+1;
  flag=_flag;
end;
run;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @kashun,

 


@kashun wrote:

I want if sum of amount1 and amount2  is greater or equal to 1 then flag+1 by ID

I assume you mean " ... to 100 ..."

data want;
set have;
by id;
if first.id then flag=.;
if amount1+amount2>=100 then flag+1;
run;

It's not clear from your example what should happen to the flag if the sum drops below 100 within a BY group. The code above retains the previous value in this case. If you want FLAG to be missing instead, use a second flag variable like this:

data want(drop=_:);
set have;
by id;
if first.id then _flag=.;
if amount1+amount2>=100 then do;
  _flag+1;
  flag=_flag;
end;
run;
kashun
Obsidian | Level 7

Thanks very much @FreelanceReinhard.

If sum drops below 100, we should have missing. Looks like your code is working.

Thanks again

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1467 views
  • 0 likes
  • 2 in conversation