2
votes

2I am writing an IF statement to filter out some values which are sequential. Is there a way to write an IF statement to bring out the sequential values

data H; 
input HH $; 
cards; 
Y1
Y2
Y3
Y4
Y5
; run; 


data t; 
set H; 
if hh in ('Y2' -'Y4');
run;
2

2 Answers

1
votes

Use the Scan function to extract the number part then filer the numbers you want:

if scan(hh,1,'Y') >= 1 & scan(hh,1,'Y') <=4;

New Datastep:

data t; 
set H; 
if  scan(hh,1,'Y') >= 1 & scan(hh,1,'Y') <=4;
run;
1
votes

You can take advantage of the fact that < and > work with character variables and sort order:

data t; 
set H; 
if 'Y2' <= hh <= 'Y4';
run;

However, Y22 would also be sorted between Y2 and Y4.

data H; 
input HH $; 
cards; 
Y1
Y2
Y3
Y4
Y5
Y22
; run; 


data t; 
set H; 
if 'Y2' <= hh <= 'Y4';
run;

So you would need to add additional logic in that case.