DATA Step, Macro, Functions and more

how come it keeps on return 'YES'?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

how come it keeps on return 'YES'?

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!

 


Accepted Solutions
Solution
‎08-26-2016 04:37 AM
Super User
Posts: 6,972

Re: how come it keeps on return 'YES'?

[ Edited ]
%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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎08-26-2016 04:37 AM
Super User
Posts: 6,972

Re: how come it keeps on return 'YES'?

[ Edited ]
%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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 242

Re: how come it keeps on return 'YES'?

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

Trusted Advisor
Posts: 1,405

Re: how come it keeps on return 'YES'?

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);

 

Super User
Posts: 9,691

Re: how come it keeps on return 'YES'?

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;

Occasional Contributor
Posts: 12

Re: how come it keeps on return 'YES'?

its my typo, my original is:

if index(x,'WARNING') > 0 then do; %let found='YES'; end
Trusted Advisor
Posts: 1,405

Re: how come it keeps on return 'YES'?

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

Super User
Posts: 9,691

Re: how come it keeps on return 'YES'?

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;


Super User
Posts: 9,691

Re: how come it keeps on return 'YES'?

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;


☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 358 views
  • 4 likes
  • 5 in conversation