MACRO code modification

Reply
Super Contributor
Super Contributor
Posts: 328

MACRO code modification

I  got correct logic (from Tom comment in my previous question) to check if same error string exist in woo.log same as in my dataset a.sas7bdat and if so then delete ...

i want to modificed below logic with DO loop with IF THEN DELETE statement... if someone can help...

i don't want to store every macro variable value (error string value) into one macro varible, "&errlist", as mentioned in below code , but wants to use different macro variable name for different error string (macro variable value)...then how can i use DO loop into it....

i also don't want to use %let statement (like %let a=this is the first error; %let b=this is the second errorSmiley Wink

--------------------------------------------------------------

filename logfile "c:\woo.log";

libname woo 'c:\woo';

proc sql no print;

select quote (trim(err_chk_logic)) into :errlist

seperated by ' '

from  woo.a;

quit;

data woo.try;

infile logfile end=eof length-length;

input err_chk_logic $varying300. length;

if err_chk_logic="error:";

if err_chk_logic in (&errlist) then delete;

run;

%put &errlist;

-------------------------------------------------------------

Super Contributor
Posts: 282

Re: MACRO code modification

Hi,

I'm not entirely clear on what your requirements are, I would suggest some sample input data and resulting output data might help.

As you want to have macro separate variables, but do not want to use %let, have you tried using call symput and the symget function?:

SAS(R) 9.2 Macro Language: Reference

SAS(R) 9.2 Macro Language: Reference

There are examples of how to use them in the links.

If this is not what you require then please also show some SAS code of how you want to put the macro variables to use, as you have already shown us how you do not want to code your SAS (and remember the sample input and output data.)

Regards,

Amir.

Super User
Posts: 10,501

Re: MACRO code modification

Code is going to fail at

if err_chk_logic="error:";

First, the case of "error" is likely incorrect, second the length of the err_chk_logic string to be any kind of useful error message, and implied by your wanting to match to another value, is going to be longer than your comparison and lastly IF the condition were true the only lines that are going to be kept for processing are the error strings and then you are trying to delete them.

You need something more like:

if substr(err_chk_logic,1,6)= "ERROR:" then do;

<checking code goes here>

end;

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

thanks for your comment...

do you think we can do this using DO loop + if then delete statement somehow...? if so what would the logic...?

....appreciate your time...

Super User
Super User
Posts: 6,502

Re: MACRO code modification

One way to processes data this way is to use PROC SQL to create multiple macro variables with numbered suffixes.

proc sql no print;

select quote (trim(err_chk_logic)) into :errlist1 - :errlist9999

from  woo.a;

%let numerr=&sqlobs;

quit;


Then later you can use a %DO loop to process the variables one by one.


%do i=1 %to &numerr ;

   if err_chk_logic = &&errlist&i  then delete;

%end;


Super User
Posts: 17,840

Re: MACRO code modification

What about a format built off the table with the errors?

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

Tom - i ran below code and got an error - i have mentioned error in comment below...Thanks!

filename logfile "c:\woo.log";

libname woo 'c:\woo';

proc sql;

quote (trim(err_chk_logic)) into :errlist1-:errlist9999

  from woo.a;

quit;

data test;

infile logfile end=eof length=length;

input err_chk_logic $varying 300. length;

%do i=1 %to &numerr;

/*

ERROR: the %do STATEMENT IS NOT VALID IN OPEN CODE.

*/

if err_chk_logic=&&errlist&i then delete;

/*

ERROR: expecting an arithmatic operator.

ERROR:the symbol is not recognize and will be ignored.

*/

%end;

/*

ERROR: the %end statement is not valid in open code

*/

run;

Super User
Super User
Posts: 6,502

Re: MACRO code modification

ERROR: the %do STATEMENT IS NOT VALID IN OPEN CODE.


Means that to use macro logic like %DO and %IF your code has to part of a macro.


* Define a macro ;

%macro test;

data test;

  infile logfile end=eof length=length;

  input err_chk_logic $varying 300. length;

%do i=1 %to &numerr;

  if err_chk_logic=&&errlist&i then delete;

%end;

run;

%mend test ;


* Execute the macro ;

%test ;


Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

/*i change some line in it and still got same error as above...*/

filename logfile "c:\woo.log";

libname woo 'c:\woo';

proc sql;

select quote (trim(err_chk_logic)) into :errlist1-:errlist9999

  from woo.a;

%let numerr=&sqlobs;

quit;

data test;

infile logfile end=eof length=length;

input err_chk_logic $varying 300. length;

%do i=1 %to &numerr;

%if err_chk_logic=&&errlist&i %then delete;

%end;

run;

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

/*** if i use simple do logic instead %do then getting error like below ***/

filename logfile "c:\woo.log";

libname woo 'c:\woo';

 

proc sql;

select quote (trim(err_chk_logic)) into :errlist1-:errlist9999

  from woo.a;

%let numerr=&sqlobs;

quit;

data test;

infile logfile end=eof length=length;

input err_chk_logic $varying 300. length;

do i=1 to &numerr;

if err_chk_logic=&&errlist&i then delete;

/*

Warning: apperent symbolic referebce I not resolved

ERROR: expecting an arithmatic operator.

*/

end;

run;

Super User
Super User
Posts: 6,502

Re: MACRO code modification

Since you are looping using data step you will need to use data step logic for retrieving the comparison text.

do i=1 to &numerr;

  if err_chk_logic= symget(cats('errlist',i)) then delete;

end;

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

in this case,

code ran fine but test.sas7bdat dataset come up with complete log as observation...

-------so i think its reading log but not deleting desired errors (same as woo.a has)...from woo.log--------

-------i think the resulted dataset, test.sas7bdat, should come up with 0 obs and 1 variable.....---------instead it has all lines from woo.log as observation----------

Thanks!

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

i have tried this one but still code is reading complete log in a.sas7bdat...

%macro test;

data test;

  infile logfile end=eof length=length;

  input err_chk_logic $varying 300. length;

do i=1 to &numerr;

  if err_chk_logic= symget(cats('errlist',i)) then delete;

end;

run;

%mend test ;


* Execute the macro ;
%test ;

I have also tried useing %do loop instaed of simple do loop, as shown below...but same result

%do i=1 %to &numerr;

  if err_chk_logic= symget(cats('errlist',i)) then delete;

%end;

Super User
Super User
Posts: 6,502

Re: MACRO code modification

Get the program to work by typing it by hand first.  Then figure out how to use code generation.

Super Contributor
Super Contributor
Posts: 328

Re: MACRO code modification

thanks Tom - this will help a lot...

Ask a Question
Discussion stats
  • 14 replies
  • 371 views
  • 0 likes
  • 5 in conversation