Desktop productivity for business analysts and programmers

need help with MACRO code plz

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 340
Accepted Solution

need help with MACRO code plz

hi SAS folks - i am new at sas and need help with macro code

Question:-

i have created one table with 1 variable and 7 observation (this table contains error name strings as observation).  i am planning to trigger this table with macro logic in such a way that if we have same string error as mention in table oservation then error will be ignored and code would run without any failure shown...

(so basically table observation as error will be ignored in log)

I would really appreciate help with logic and code if someone can provide...

Thanks in advance...


Accepted Solutions
Solution
‎09-03-2013 07:34 PM
Super User
Super User
Posts: 6,849

Re: need help with MACRO code plz

Again. Write a program that works for one string.  A macro is a code generator. You have to know what code you want to generate before you can "macrotize" it.

For example if you logic included the line:

if string in : ("ERROR: File WORK.Y.DATA does not exist.") then delete;     

Then you could make a dataset that had that text in it.

data a;

  errstring = "ERROR: File WORK.Y.DATA does not exist." ;

run;

Then you could pull that string out into a macro variable that your program could reference.

proc sql noprint;

  select quote(trim(errstring)) into :errlist separated by ',' from a;

quit;


...

if string in : (&errlist) then delete;     

...

I doubt that your program will end up being that simple, but the idea is the same.  Figure out what code you want to generate, then you can work on generating it.


View solution in original post


All Replies
Super User
Super User
Posts: 6,849

Re: need help with MACRO code plz

Not sure what you mean.

Do you want to know how to query a table to find out if a given macro variable matches the value of one of the variables?

proc sql noprint ;

select var from my.table where var="&mvar" ;

quit;

%if (&sqlobs) %then %put Found a match ;

%else %put No match found. ;

Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

Thanks for your quick response...

no Tom - Macro code would check the one particular log file (created by another sas program and macro code i am talking about would dump into this program) and if log file has same error as mentioned in the table (that i already created with exception error's name, 1 variable and 7 observation - here observations are error's name) then macro code would refer/match/trigger that table (which has 7 exception error) and error will ignored (since error string match with table) and SAS code will continue to run to finish rest...

hope this will clear you doubts...

Super User
Posts: 11,134

Re: need help with MACRO code plz

I would submit that reading a log is kind of late for graceful error handling. The SAS automatic SYSERR variable available after many procedures and datastep might be a more profitable place to spend coding time than trying to parse Log text.

Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

i am not sure what would be good whether macro or data steps but if someone share logic/code with macro/data step would be grt...

Valued Guide
Posts: 3,208

Re: need help with MACRO code plz

There is a lot documented, but difficult to find, a good starting point is:

SAS(R) 9.4 Language Reference: Concepts ( Error Processing and Debugging )

Having this it is more easier to find it elsewhere as it everywhere coming back. 

SAS(R) 9.4 Macro Language: Reference (automatic macro vars)

The OS level (batch processing) can get the completion code (companiums).

SQL has a lot on it, SAS/connect has....

---->-- ja karman --<-----
Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

my logic would be like this...

/*

%macro woo;

filename logfile "c:\abcd\woo.log;

if string=:"error:" into %logfile is eq variable_name into sas dataset then delete;

/* here variable_name variable is contains error name string which could occure in logfile and i want to delete those error string*/

run;

%mend woo;

*/

/*invoke macro*/

%woo;

but still i am not getting correct code to do this...so if someone can help...

Thanks!

Super User
Posts: 19,171

Re: need help with MACRO code plz

I think you need to share more details about what you're trying to do.


Is the SAS code generating an error and then failing to continue the process?

Is there macros involved?

Is this multiple programs running in Batch?

Is it for a specific program for a specific row for example or for multiple programs?

See here:

Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

I am running sas 9.1.3 on windows 2003.

one sas program running in batch, lets say 'woo.sas', and its log, "woo.log" has some error as mentioned below,

error: this is the one error

error: this is the second error

error: this is the third error

.

.

.

error: this is the seventh error

/*

i wanted to delete all these errors (from "woo.log") with a logic using macro or data step or something like that...in order to do that i have done following thing

/*step-1*/

/*have created table 'A' wich has all above seven error in it/*

A

error::this is the first error

error: this is the second error

error: this is the third error

.

.

.

error: this is the seventh error

/*step2*/

/*want to write code/logic which will excute right after "woo.sas" and check "woo.log" and if  "woo.log" has same error as in table  A (that i have cretaed) then all error will be deleted */

/*so basically a macro which would point table 'A' and if woo.log has same error as table 'A' then all errors will be deleted like below - but not getting code right  

  way*/

%macro woo;

filename logfile "c:\abcd\woo.log;

if string=:"error:" into %logfile is eq variable_name into table A then delete;

/* here variable_name variable is contains error name string which could occure in woo.log and i want to delete those error string*/

run;

%mend woo;

/*invoke macro*/

%woo;

/*Thanks a lot*/

Super User
Super User
Posts: 6,849

Re: need help with MACRO code plz

You still need to describe more the purpose of the activity.  Sounds like you want to post-process a SAS log and somehow indicate that it is ok to ignore selected error messages.  What actions do you want to perform?  Sounds like you want to produce a modified version of the log file.  Do you also want to produce a status summary such as number of unacceptable error messages?

First figure out how to process one file to locate one example error message.  This probably needs to be reasonably flexible as SAS error messages usually have some variable information in them such as line number, variable name or dataset name.  Here is a trivial example to ignore errors about missing WORK.Y dataset.

data _null_;

  if eof then call symputx('nerrors',nerrors);

  infile 'woo.log' end=eof ;

  file 'woo.log_fixed';

  input ;

  if _infile_ =: 'ERROR' then do;

      if _infile_ = 'ERROR: File WORK.Y.DATA does not exist.' then _infile_='IGNORE ' || _infile_;

     else nerrors+1;

  end;

  put _infile_ ;

run;

Then figure out how to handle multiple error messages.

Then figure out how to generate the code that works for multiple messages from an input dataset or input macro variables.

At that point you are ready to begin writing your macro.

Super User
Posts: 5,369

Re: need help with MACRO code plz

There is an automatic macro variable you can use:  &SYSERRORTEXT

You will have to make sure that the values in your table match what would be contained in &SYSERRORTEXT (or could be mapped to it, if need be).

A macro could easily generate this statement to end your program/session if a new error message (one that is not part of your table) is found:

endsas;

Of course, you will have to invoke the macro many times ... after each DATA or PROC step, perhaps even more frequently since each SELECT statement within PROC SQL could generate an error.

Good luck.

Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

i wants to delete all error messages that woo.log might have and may be same as table 'A' has...............so in batch i will run woo.sas first then new_code.sas (new_code.sas may be has same logic as you just mentioned).........and in batch it will go like this.......

"C:\...\sas.exe" -sysin c:\...\woo.sas -log c:\...\log -CONFIG "C:\...\SASV9.CFG" -NOPRINT

"C:\...\sas.exe" -sysin c:\...\new_code.sas

/*currently so woo.sas has %let syscc=0; (at end of the code) and also has some error messages in woo.log and printing log message at end of the log like

  error: errors printed on pages 1234, 5678. errors printed on pages 123456, 789101.

  (THEN - new_code.sas will run with logic (may be same as you mention) - trigger table 'A' - mathing woo.log error with table 'A' and  then DELETE/IGNORE all 

  those error in woo.log - (all these errors are exceptional error in our business definition) ----Thanks

*/

Super User
Posts: 19,171

Re: need help with MACRO code plz

What are the types of errors you're trying to avoid printing? You can control the level of errors that sas prints.

It does sound like a modified log issue, so scan your log, find an error. Then look up the error, my guess is using a format, rather than any merge.

Then delete that row rather than output is my guess.

What exactly do you need help with?

There are log parsers online that search for error, so adding in a format to see if the error matches should be relatively easy.

Roland has an example here:

http://www.datasavantconsulting.com/roland/scanlog.sas

Super Contributor
Super Contributor
Posts: 340

Re: need help with MACRO code plz

Let me put it this way...I am sorry if i am not explaining it at my best...i really need help on this...

this is where i am wirte now

/*   have created one sas dataset 'A' with 1 variable and 7 exception errors     */

/*   now i am planning to write a sas program as below which will trigger A.SAS7BDAT after woo.sas complete to check if woo.log has same error as A dataset errors and then dataset one will have 0 obs and 1 variable after reading records from logfile, woo.log   */

/*here for me mystry part is highlighted with BOLD  in code */

/*   location of the log file   */                                                                              

filename logfile "c:\...\woo.log"

%macro woo;

/*   if log file doesn't exist pass error to user    */                                             

%if %sysfunc(fileexist(logfile) eq 0 %then %let syscc=100;

/*   now reading woo.log file   */                                                                       

data one;  

infile logfile end=eof;

input string $varying500. length;

if string=: "error:" eq A.sas7bdat variable then delete;      /*   here A.sas7bdat has same error as woo.log may have...- i have created A sas data set   */

run;

/*determine number of errors using PROC SQL   */

                             

/*   if there are any errors - pass an errors to scheduler   */

%if &count ne 0 %then %let syscc=100;

%mend woo;

%woo;

/* 

Solution
‎09-03-2013 07:34 PM
Super User
Super User
Posts: 6,849

Re: need help with MACRO code plz

Again. Write a program that works for one string.  A macro is a code generator. You have to know what code you want to generate before you can "macrotize" it.

For example if you logic included the line:

if string in : ("ERROR: File WORK.Y.DATA does not exist.") then delete;     

Then you could make a dataset that had that text in it.

data a;

  errstring = "ERROR: File WORK.Y.DATA does not exist." ;

run;

Then you could pull that string out into a macro variable that your program could reference.

proc sql noprint;

  select quote(trim(errstring)) into :errlist separated by ',' from a;

quit;


...

if string in : (&errlist) then delete;     

...

I doubt that your program will end up being that simple, but the idea is the same.  Figure out what code you want to generate, then you can work on generating it.


🔒 This topic is solved and locked.

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

Discussion stats
  • 27 replies
  • 778 views
  • 1 like
  • 6 in conversation