BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mazouz
Calcite | Level 5
data have;
input var1 6.2 var2 6.2 var3 6.2;
datalines;
1400.00 165.55 839.57
0.00 0.00 106.42
1200.00 142.98 725.09
0.00 0.00 91.91
270.00 0.00 168.85
0.00 0.00 67.54
0.00 0.00 33.61
270.00 0.00 0.00
0.00 0.00 168.85
0.00 0.00 101.15
155.00 1.70 150.00
0.00 0.00 3.30
;
run;
Hello I want to verify if var1 < (var2+var3) by group (while I have 0.00) (1)
exemple: 1400.00 with 165.55 + 839.57 +106.42
1200.00 with 142.98 + 725.09 + 91.91
270.00 with 168.85 + 67.54 +33.61
want = new column 1 if i Have (1)
0 else

 

1 ACCEPTED SOLUTION

Accepted Solutions
Angel_Larrion
SAS Employee

This works, but there is not a single case in the "have" table that meets condition (1).

data have;
input  var1   var2   var3 ;
datalines;
1400.00 165.55  839.57
0.00	0.00	106.42
1200.00	142.98	725.09
0.00	0.00	91.91
270.00	0.00	168.85
0.00	0.00	67.54
0.00	0.00	33.61
270.00	0.00	0.00
0.00	0.00	168.85
0.00	0.00	101.15
155.00	1.70	150.00
0.00	0.00	3.30
;
run;

data have1;
set have;
retain group 0;
if var1 ne 0 then group=group+1;
run;

data want;
set have1;
by group;
retain sum_var1 0 sum_var2_var3 0;

if first.group and last.group=0 then do;
sum_var1=var1; 
sum_var2_var3=sum(var2,var3);
end;

else if first.group=0 and last.group=0 then do;
sum_var1=sum(sum_var1,var1); 
sum_var2_var3=sum(sum_var2_var3,sum(var2,var3));
end;

else do;
sum_var1=sum(sum_var1,var1); 
sum_var2_var3=sum(sum_var2_var3,sum(var2,var3));
if sum_var1 < sum_var2_var3 then new=1;
else new=0;
end;
drop sum_var1 sum_var2_var3;
run;

 

View solution in original post

2 REPLIES 2
ballardw
Super User

You really need to add a variable to your data that indicates which "group" a set of records belongs to or explicitly define a rule that will let us know when a group begins. You don't provide that currently.

 

Otherwise there really isn't a consistent way to see what a "group" may actually be.

 

You also need to show exactly how you expect your result to appear.

Angel_Larrion
SAS Employee

This works, but there is not a single case in the "have" table that meets condition (1).

data have;
input  var1   var2   var3 ;
datalines;
1400.00 165.55  839.57
0.00	0.00	106.42
1200.00	142.98	725.09
0.00	0.00	91.91
270.00	0.00	168.85
0.00	0.00	67.54
0.00	0.00	33.61
270.00	0.00	0.00
0.00	0.00	168.85
0.00	0.00	101.15
155.00	1.70	150.00
0.00	0.00	3.30
;
run;

data have1;
set have;
retain group 0;
if var1 ne 0 then group=group+1;
run;

data want;
set have1;
by group;
retain sum_var1 0 sum_var2_var3 0;

if first.group and last.group=0 then do;
sum_var1=var1; 
sum_var2_var3=sum(var2,var3);
end;

else if first.group=0 and last.group=0 then do;
sum_var1=sum(sum_var1,var1); 
sum_var2_var3=sum(sum_var2_var3,sum(var2,var3));
end;

else do;
sum_var1=sum(sum_var1,var1); 
sum_var2_var3=sum(sum_var2_var3,sum(var2,var3));
if sum_var1 < sum_var2_var3 then new=1;
else new=0;
end;
drop sum_var1 sum_var2_var3;
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
  • 2 replies
  • 507 views
  • 0 likes
  • 3 in conversation