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

I would like to understand how to use 'not in' in SCAN function. What would be wrong in the below code?

 

5th word from filename is '0417' which is not equals to either '5601' or '6010' or '6020'.So I want value of source=filename in my output but I'm getting source='missing' in my output.

 

%let function=NL; 

data have;
filename = "IFT_GT_RND_2_0417_1_20201009T075212.csv";
if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and not scan(lowcase(filename),5,'_') in ('5601','6010','6020') then do;
      source=filename;
end;
else source='missing';
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Maxim 2: Read the Log:

 73         %let function=NL;
 74         
 75         data have;
 76         filename = "IFT_GT_RND_2_0417_1_20201009T075212.csv";
 77         if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and not scan(lowcase(filename),5,'_') in
 77       ! ('5601','6010','6020') then do;
 78               source=filename;
 79         end;
 80         else source='missing';
 81         run;
 
 NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
       77:69   77:73   

This points to the fact that the NOT operator is resolved first, before the IN. So SAS needs to make a boolean (numeric) value out of the result of the character SCAN function.

There are two ways to make this correct:

if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and not (scan(lowcase(filename),5,'_') in ('5601','6010','6020')) then do;

or

if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and scan(lowcase(filename),5,'_') not in ('5601','6010','6020') then do;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

Maxim 2: Read the Log:

 73         %let function=NL;
 74         
 75         data have;
 76         filename = "IFT_GT_RND_2_0417_1_20201009T075212.csv";
 77         if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and not scan(lowcase(filename),5,'_') in
 77       ! ('5601','6010','6020') then do;
 78               source=filename;
 79         end;
 80         else source='missing';
 81         run;
 
 NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
       77:69   77:73   

This points to the fact that the NOT operator is resolved first, before the IN. So SAS needs to make a boolean (numeric) value out of the result of the character SCAN function.

There are two ways to make this correct:

if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and not (scan(lowcase(filename),5,'_') in ('5601','6010','6020')) then do;

or

if scan(lowcase(filename),-1,'.')='csv' and "&function" EQ 'NL' and scan(lowcase(filename),5,'_') not in ('5601','6010','6020') then do;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1 reply
  • 1339 views
  • 1 like
  • 2 in conversation