I have a dataset similar to this. I want to compare fcst_val and val variables and retain the instance of desc column where the fcst_val less than or equal to the val.
Here is an example:
data have;
input type $ desc $ mo fcst_val val;
datalines;
A D2 0 100 1000
A D1 1 100 1050
A B1 2 100 1100
B D1 0 200 150
B B0 1 200 250
B D2 2 200 300
B B1 3 200 350
c D2 0 400 10
C D1 1 400 300
C B1 1 400 400
;
run;
data want;
input type $ desc $ mo fcst_val val new_val;
datalines;
A D2 0 100 1000 D2
A D1 1 100 1050 D2
A B1 2 100 1100 D2
B D1 0 200 150 B0
B B0 1 200 250 B0
B D2 2 200 300 B0
B B1 3 200 350 B0
c D2 0 400 10 B1
C D1 1 400 300 B1
C B1 1 400 500 B1
;
run;
Hi @zqkal
data have;
input type $ desc $ mo fcst_val val;
datalines;
A D2 0 100 1000
A D1 1 100 1050
A B1 2 100 1100
B D1 0 200 150
B B0 1 200 250
B D2 2 200 300
B B1 3 200 350
C D2 0 400 10
C D1 1 400 300
C B1 1 400 500
;
run;
data want;
do until(last.type);
set have;
by type;
length new_val $10;
if fcst_val<val and missing(new_val) then new_val=desc;
end;
do until(last.type);
set have;
by type;
output;
end;
run;
Hi @zqkal
data have;
input type $ desc $ mo fcst_val val;
datalines;
A D2 0 100 1000
A D1 1 100 1050
A B1 2 100 1100
B D1 0 200 150
B B0 1 200 250
B D2 2 200 300
B B1 3 200 350
C D2 0 400 10
C D1 1 400 300
C B1 1 400 500
;
run;
data want;
do until(last.type);
set have;
by type;
length new_val $10;
if fcst_val<val and missing(new_val) then new_val=desc;
end;
do until(last.type);
set have;
by type;
output;
end;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.