DATA Step, Macro, Functions and more

data and Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

data and Macro

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.


Accepted Solutions
Solution
‎04-17-2017 04:43 PM
Super User
Posts: 5,497

Re: data and Macro

Posted in reply to Melvin_Sas

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


All Replies
Valued Guide
Posts: 634

Re: data and Macro

Posted in reply to Melvin_Sas

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');
PROC Star
Posts: 7,467

Re: data and Macro

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

 

Occasional Contributor
Posts: 10

Re: data and Macro

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;

Valued Guide
Posts: 634

Re: data and Macro

Posted in reply to Melvin_Sas
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.
Occasional Contributor
Posts: 10

Re: data and Macro

Thanks Artc

Solution
‎04-17-2017 04:43 PM
Super User
Posts: 5,497

Re: data and Macro

Posted in reply to Melvin_Sas

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

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 182 views
  • 0 likes
  • 4 in conversation