DATA Step, Macro, Functions and more

create a flag

Reply
Super Contributor
Posts: 673

create a flag

I'm using union all on two data sources so that record from data source is arranged sequentially (one after the other,based on the sort condition).How to create a flag 'Y' if the records from each datasource have the same value.for instance:

source Outlet price1 price2
DDD 08536148 350 260
CVS 08852148 350 260
DDD 08536148 1000 400
CVS 08852148 350 260


for the first two records we need to set flag to Y as they are the same.
Trusted Advisor
Posts: 2,116

Re: create a flag

You could just SORT by price1 and price2 and then use BY processing to assign the flag. That would work for your direct question. I suspect that there is more to it that you haven't shared.
Super Contributor
Posts: 673

Re: create a flag

source Outlet price1 price2
DDD 08536148 350 260
CVS 08536148 350 260
DDD 08852148 1000 400
CVS 08852148 350 260


Yes,
I gave the order of the outlets wrongly in the OP.for the same outlets from different sources we need to set a flag if their prices are equal.
as in the case of first two records.
Trusted Advisor
Posts: 2,116

Re: create a flag

PROC SORT; BY outlet price1 price2; RUN;

DATA;
SET;
By outlet price1 price2;
IF first.price2 & last.price2 THEN Flag='No '; * unique record;
ELSE flag='Yes'; * two or more are the same;
RUN;
Super User
Posts: 10,041

Re: create a flag

It is almost the same with Doc@Duck, except to remove repetition observations.
[pre]
data temp;
input source $ Outlet : $20. price1 price2;
cards;
DDD 08536148 350 260
CVS 08536148 350 260
DDD 08852148 1000 400
CVS 08852148 350 260
;
run;

proc sort data=temp nodupkey;
by outlet price1 price2 source ;
run;
data op;
set temp;
By outlet price1 price2 ;
length flag $ 1;
if not (first.price2 and last.price2 ) then flag='Y';
run;
[/pre]



If you allow the existence of repetition obs then will need some more code.


Ksharp
Ask a Question
Discussion stats
  • 4 replies
  • 157 views
  • 0 likes
  • 3 in conversation