Dear SAS-Community
I'm failing with a problem that actually sounds not very complicated:
I have a data table of the form as I've attached. For the case that you cannot open it, it looks like this:
Code year value
10 2015 375
10 2016 640
10 2017 710
20 2014 0
20 2015 125
20 2016 950
20 2017 0
What I like to do is the following: If the value of the line with code = 20 and year = 2017 is zero (as in my example), then I like to set the values of code 20 (only code 20!) for all years to zero. In my example this would mean that instead of the values 125 and 950 there should be written zero.
I'm very grateful for every help!
Best regards, Leo
Since 2017 is the largest value sort it descending.
Then 2017 will be the first value.
proc sort data=have;
by code descending year;
run;
data want;
set have;
retain flag;
if first.code and year=2017 and value=0 then flag=1;
else if first.code and year=2017 and value ne 0 then flag=0;
if flag=1 then value=0;
run;
Since 2017 is the largest value sort it descending.
Then 2017 will be the first value.
proc sort data=have;
by code descending year;
run;
data want;
set have;
retain flag;
if first.code and year=2017 and value=0 then flag=1;
else if first.code and year=2017 and value ne 0 then flag=0;
if flag=1 then value=0;
run;
Thank you very much, Reeza!
Best regards, Leo
data have;
input Code year value ;
cards;
10 2015 375
10 2016 640
10 2017 710
20 2014 0
20 2015 125
20 2016 950
20 2017 0
;
proc sql;
create table want as
select code,year ,abs(max(value = 0 and year=2017)-1)*value as value
from have
group by code
order code,year;
quit;
To me, this would be the most intuitive approach:
data want;
if _n_=1 then do;
set have (where=(year=2017 and code=20));
if value=0 then reset=1;
retain reset;
end;
set have;
if code=20 and reset=1 then value=0;
drop reset;
run;
This assumes that you always have one observation where YEAR is 2017 and CODE is 20.
data have;
input Code year value ;
cards;
10 2015 375
10 2016 640
10 2017 710
20 2014 0
20 2015 125
20 2016 950
20 2017 0
;
data want;
do until(last.code);
set have;
by code;
if year=2017 and value=0 then yes=1;
end;
do until(last.code);
set have;
by code;
if yes then value=0;
output;
end;
drop yes;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.