BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zqkal
Obsidian | Level 7

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;

zqkal
Obsidian | Level 7
Thanks for your help

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
  • 2 replies
  • 736 views
  • 1 like
  • 2 in conversation