BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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.
4 REPLIES 4
Doc_Duke
Rhodochrosite | Level 12
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.
SASPhile
Quartz | Level 8
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.
Doc_Duke
Rhodochrosite | Level 12
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;
Ksharp
Super User
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

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
  • 4 replies
  • 1397 views
  • 0 likes
  • 3 in conversation