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!
%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.
%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.
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
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);
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;
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
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;
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: