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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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