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

Hi all, I am new to SAS and now writing a sas code to determine if 'WARNING' was found inside the tmp.log. However, it keeps on return 'YES'. I don't really get that. Please kindly give advice below questions. 

Thanks in advance.

 

Question 1)

%let found='NO';

 

data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;


if index(x,'WARNING')< 0 then do; %let found='YES'; end;
run;

 

data _null_;
if &found.=YES then put 'OK...I HAVE FOUND';
run;

 

tmp.log:

hello

hello

hello

 

Question 2) 

Whats different between

%let found = 'YES'  and

%let found = YES

 

 

THANKS!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%let found='NO';

data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;
if index(x,'WARNING')< 0 then do; %let found='YES'; end;
run;

data _null_;
if &found.=YES then put 'OK...I HAVE FOUND';
run;

The most important thing:

The macro language is a pre-processing language designed to create dynamic program code.

Therefore, everytime a macro trigger (& or %) is encountered, the macro processor is envoked, does its thing, and then comes back with or without program text for the main SAS interpreter.

This means that your %let found='YES'; is immediately dealt with when program text is collected for the data step compiler, setting found to 'YES'. No program text is generated, so the data step looks like that:

data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;
if index(x,'WARNING')< 0 then do; end;
run;

If you want to set a macro variable from the execution of the data step, use call symput().

 

The answer to question two: &found will just contain text, once with and once without quotes. Take care if quotes are appropriate/necessary where you use &found.

 

A much nicer version of writing your checker would be

data _null_;
retain signal 0;
infile '/folders/myfolders/myprograms/tmp.log' truncover end=done;
input x $1025.;
if index(x,'WARNING') > 0 then signal = 1;
if done and signal then put 'YES';
run;

OTOH, I would use grep on the OS level for this. But I'm just an old UNIXer.

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User
%let found='NO';

data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;
if index(x,'WARNING')< 0 then do; %let found='YES'; end;
run;

data _null_;
if &found.=YES then put 'OK...I HAVE FOUND';
run;

The most important thing:

The macro language is a pre-processing language designed to create dynamic program code.

Therefore, everytime a macro trigger (& or %) is encountered, the macro processor is envoked, does its thing, and then comes back with or without program text for the main SAS interpreter.

This means that your %let found='YES'; is immediately dealt with when program text is collected for the data step compiler, setting found to 'YES'. No program text is generated, so the data step looks like that:

data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;
if index(x,'WARNING')< 0 then do; end;
run;

If you want to set a macro variable from the execution of the data step, use call symput().

 

The answer to question two: &found will just contain text, once with and once without quotes. Take care if quotes are appropriate/necessary where you use &found.

 

A much nicer version of writing your checker would be

data _null_;
retain signal 0;
infile '/folders/myfolders/myprograms/tmp.log' truncover end=done;
input x $1025.;
if index(x,'WARNING') > 0 then signal = 1;
if done and signal then put 'YES';
run;

OTOH, I would use grep on the OS level for this. But I'm just an old UNIXer.

RahulG
Barite | Level 11

1.

I think it should be GREATER THAN SYMBOL

if index(x,'WARNING') > 0 

 

%let found='NO';
 
data _null_;
infile '/folders/myfolders/myprograms/tmp.log' dlm=' ' truncover;
input x $1025.;

if index(x,'WARNING') >  0 then do; 
CALL SYMPUTX('FOUND','YES');
 end;
run;
 
data _null_;
if &found.=YES then put 'OK...I HAVE FOUND';
run;

 

2.

%let found = 'YES'   and  %let found = YES has difference.

 

first one will have quotes around YES. Second one would not have quotes around YES

Shmuel
Garnet | Level 18

change line:

    if index(x,'WARNING') < 0 then do; %let found='YES'; end;

 

to: 

   if index(x,'WARNING') > 0 then do; %let found='YES'; end;

 

The INDEX function returns the position of starting string. That can be 0 or positive only.

If the string is not found the INDEX will be 0 (zero);

 

Ksharp
Super User
It could be simple as :


data _null_;
infile '/folders/myfolders/myprograms/tmp.log' ;
input ;
if x =: 'WARNING' then do; putlog 'FOUND:';  putlog _infile_; end;
run;

Olime
Fluorite | Level 6
its my typo, my original is:

if index(x,'WARNING') > 0 then do; %let found='YES'; end
Shmuel
Garnet | Level 18

OOPS! the others are right:

   %LET is a declarative statment that cannot be a part of IF / WHEN / any insteam data step code.

 

Sorry for the misinform

Ksharp
Super User
Opps.CODE NOT TESTED


data _null_;
infile '/folders/myfolders/myprograms/tmp.log' end=last;
input ;
retain found 0;
if _infile_ =: 'WARNING' then do; putlog 'FOUND:';  putlog _infile_; found=1; end;
if last and not found then putlog 'OK';
run;


Ksharp
Super User
Opps.CODE NOT TESTED


data _null_;
infile '/folders/myfolders/myprograms/tmp.log' end=last;
input ;
retain found 0;
if _infile_ =: 'WARNING' then do; putlog 'FOUND:';  putlog _infile_; found=1; end;
if last and not found then putlog 'OK';
run;


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
  • 8 replies
  • 2305 views
  • 4 likes
  • 5 in conversation