In words here are the rules I want to implement:
If Flag = N then WANT = HAVE
If Flag = Y and HAVE value is max among observations with same ID, then WANT=HAVE
If Flag = Y and above rule is false, then WANT=.5*HAVE
If there's an instance where two values for an ID are max (and Flag=Y), then arbitrarily WANT = HAVE for one and WANT = .5*HAVE for the other. (this is shown for ID b below)
Thanks in advance.
proc sort data=have;
by ID flag have;
run;
data want;
set have;
by ID flag have;
if Flag = 'N' then want=have;
else if flag ='Y' and last.flag then want=have;
else want =.5*have;
run;
proc sort data=have;
by ID flag have;
run;
data want;
set have;
by ID flag have;
if Flag = 'N' then want=have;
else if flag ='Y' and last.flag then want=have;
else want =.5*have;
run;
Then kindly mark the question as answered.
You need clarify something more .
What If there's an instance where THREE values for an ID are max (and Flag=Y), What you are going to do ?
And Don't post it as picture, post it as TEXT . No one would like to type it for you and would ignore your quesiton.
data have;
input id $ flag $ have ;
cards;
a y 8437
a y 4533
a n 6814
b n 5322
b y 8031
b y 8031
;
run;
data want;
do until(last.id);
set have;
by id;
max=max(max,have);
end;
do until(last.id);
set have;
by id;
if flag='n' then want=have;
if flag='y' and have ne max then want=.5*have;
if flag='y' and have=max then do;
if found then want=.5*have;
else do;want=have;found=1;end;
end;
output;
end;
drop max;
run;
It is hard to say something , it is all depend on what you want.
And There looks like some problem in his code :
data have;
input id $ flag $ have ;
cards;
a y 8437
a y 4533
a n 6814
b n 9322
b y 8031
b y 8031
;
proc sort data=have;
by ID flag have;
run;
data want;
set have;
by ID flag have;
if Flag = 'n' then want=have;
else if flag ='y' and last.flag then want=have;
else want =.5*have;
run;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.