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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

View solution in original post

6 REPLIES 6
ArtC
Rhodochrosite | Level 12

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');
art297
Opal | Level 21

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

 

Melvin_Sas
Fluorite | Level 6

Thanks Atrc.. I tried the below code but the value is always success,

 

Checkerror Dataset:

 

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

ArtC
Rhodochrosite | Level 12
Include a SET statement in your second step. You are not reading any data, the variable ERROR is always missing. Move the %PUT until after the RUN;. A macro %PUT cannot be conditionally executed within the DATA step.
Melvin_Sas
Fluorite | Level 6

Thanks Artc

Astounding
PROC Star

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

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
  • 6 replies
  • 2175 views
  • 0 likes
  • 4 in conversation