I need to assign values to global macro variable based on a if condition , But it fails with error
ERROR 180-322: Statement is not valid or it is used out of proper order.
%global Status;
data Hello;
Infile 'C:\Users\FSAX.txt' truncover;
input a_line $2000. ;
if index(a_line, 'ERROR:') > 0 then do;
Main = 1;
end;
If Main = 1 then
%Let Status = 'Failed : ';
run;
This macro varibale Status is used in other macro functions.
Combining a number of suggestions (and adding 1 or 2 more):
data _null_;
Infile 'C:\Users\FSAX.txt' truncover;
input a_line $2000. ;
if index(a_line, 'ERROR:') > 0 then do;
call symput('Status', 'Failed : ');
stop;
end;
run;
You don't really need to create a SAS data set (hence _NULL_), and you don't need to continue the DATA step once a single "ERROR:" is found (hence STOP).
Macro language elements (like the %LET statement) cannot be conditionally executed by DATA step IF-THEN/ELSE. Instead use the DATA step's SYMPUTX routine to make the assignment. The IF statement becomes:
If Main = 1 then call symputx('status','Failed','g');
Expanding on @ArtC's comment, I'd also include a check so that only one attempt is needed to create the macro variable. e.g.:
data Hello; Infile 'C:\Users\FSAX.txt' truncover; input a_line $2000. ; if index(a_line, 'ERROR:') > 0 then Main = 1; If eof and Main = 1 then call symputx('Status','Failed : ','g'); run; %put &Status.;
Art, CEO, AnalystFinder.com
Thanks Atrc.. I tried the below code but the value is always success,
Checkerror Dataset:
a_line | Error |
This is a dealership account ERROR: expires only by Dec'2016. | 1
|
%global Status;
data CheckError;
Infile 'C:\Users\FSAX.txt' truncover;
input a_line $2000. ;
if index(a_line, 'ERROR:') > 0 then do;
Error = 1;
end;
If Error = 1;
RUN;
Data CheckError;
If Error = 1 then
call symputx('Status','Failed : ','g');
Else
call symputx('Status','Success : ','g');
%put &Status.;
run;
Thanks Artc
Combining a number of suggestions (and adding 1 or 2 more):
data _null_;
Infile 'C:\Users\FSAX.txt' truncover;
input a_line $2000. ;
if index(a_line, 'ERROR:') > 0 then do;
call symput('Status', 'Failed : ');
stop;
end;
run;
You don't really need to create a SAS data set (hence _NULL_), and you don't need to continue the DATA step once a single "ERROR:" is found (hence STOP).
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.