Hello,
I have the test table and I want to have the table test1.
The objectif is to delete the lines that are annulled
Thank you for your help
data test;
input var1 $ var2;
cards;
A 200
A 100
A -200
A 200
B 100
;
run;
data test1;
input var1 $ var2;
cards;
A 200
A 100
B 100
;
run;
data test;
input var1 $ var2;
cards;
A 200
A 100
A -200
A 200
B 100
;
run;
data p(index=(x=(var1 var2))) n(index=(x=(var1 var2)));
set test;
if var2>0 then output p;
else do;var2=-var2;output n;end;
run;
data want;
ina=0;inb=0;
merge p(in=ina) n(in=inb);
by var1 var2;
if not ina or not inb;
run;
data test;
input var1 $ var2;
cards;
A 200
A 100
A -200
A 200
B 100
;
run;
proc sort data=test out=want nodupkey;
by var1 var2;
where var2>0;
run;
That's not enough information. What indicates annulment? Is it only when the previous record exactly matches this one, but minus? What about:
a 100
a 100
1 -200
Is this an annullment?
What are the ordering variables here?
This works, not sure it will cover all eventualities though:
data test; input var1 $ var2; cards; A 200 A 100 A -200 A 200 B 100 ; run; data want; set test; retain newdiff flag; by var1; if first.var1 then do; newdiff=var2; flag=0; end; else if newdiff+var2=0 then flag=1; if flag=1 then do; flag=2; delete; end; if flag=2 then do; flag=0; delete; end; run;
data test;
input var1 $ var2;
cards;
A 200
A 100
A -200
A 200
B 100
;
run;
data p(index=(x=(var1 var2))) n(index=(x=(var1 var2)));
set test;
if var2>0 then output p;
else do;var2=-var2;output n;end;
run;
data want;
ina=0;inb=0;
merge p(in=ina) n(in=inb);
by var1 var2;
if not ina or not inb;
run;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.