BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

Here is a way to return a value inside a MACRO. 

 

But how to return a specific value, say -1, when the macro failed in middle?!

 

My macro simply reads a delimited file into a dataset in an automatic process; somehow it fails occasionally. I need know whether failed or not

and plan to re-read it seconds later if failed.  

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So the question is no longer how to return something from a macro execution.

Now the question is how to tell when a step fails.

 

Since you are using PROC IMPORT that already creates a macro variable as part of the SAS code it generates.  Check the SAS log for one your runs (with MPRINT option on of course) to see the macro variable name.

 

So you might be able to use that macro variable.

%macro readin(dt, pt);
PROC IMPORT OUT=pgap_p&pt.
     DATAFILE="C:\Users\Administrator\Desktop\data\1min__2022&dt._&pt..log"
     DBMS=CSV REPLACE
;
   GETNAMES=NO;
   DATAROW=1;
RUN;
%mend;

%* Give the macro variable a value so it exists outside the macro scope ;
%let _EFIERR_= Before macro call;
%* Call the macro ;
%readin(dt=0606,pt=10)
%* Test the value of the macro variable ;
%put &=_EFIERR_ ;

Or you could just test if the dataset is created or not.

%put %sysfunc(exist(pgap_p10));

 

PS    Why would you use PROC IMPORT?  Especially if the file does not have a header row.  Does the structure of the file change?  Why do you need to use a guessing program to read it?

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

So what is the failure? Data set not created? Zero obs in data set? Character variables where it should be numeric? Something else?

 

Look at %ABORT CANCEL -1;

--
Paige Miller
hellohere
Pyrite | Level 9

Let me get the log later since it does not happen every time. The Macro only has Proc IMPORT. 

 

The issue is that the to-read-in file is being written in real time by C++. 

 

%macro readin(dt, pt);
	/*%let dt=0606; %let pt=10;*/
	PROC IMPORT OUT=pgap_p&pt.
	     DATAFILE="C:\Users\Administrator\Desktop\data\1min__2022&dt._&pt..log"
	   DBMS=CSV REPLACE;
	   GETNAMES=NO;
	   DATAROW=1;
	RUN;
%mend;
Tom
Super User Tom
Super User

So the question is no longer how to return something from a macro execution.

Now the question is how to tell when a step fails.

 

Since you are using PROC IMPORT that already creates a macro variable as part of the SAS code it generates.  Check the SAS log for one your runs (with MPRINT option on of course) to see the macro variable name.

 

So you might be able to use that macro variable.

%macro readin(dt, pt);
PROC IMPORT OUT=pgap_p&pt.
     DATAFILE="C:\Users\Administrator\Desktop\data\1min__2022&dt._&pt..log"
     DBMS=CSV REPLACE
;
   GETNAMES=NO;
   DATAROW=1;
RUN;
%mend;

%* Give the macro variable a value so it exists outside the macro scope ;
%let _EFIERR_= Before macro call;
%* Call the macro ;
%readin(dt=0606,pt=10)
%* Test the value of the macro variable ;
%put &=_EFIERR_ ;

Or you could just test if the dataset is created or not.

%put %sysfunc(exist(pgap_p10));

 

PS    Why would you use PROC IMPORT?  Especially if the file does not have a header row.  Does the structure of the file change?  Why do you need to use a guessing program to read it?

hellohere
Pyrite | Level 9

Create a macro before and after Proc IMPORT would be a good try, I BET. 

 

To check the dataset is created or not is not, since I need read the same file with same output dataset periodically. 

Quentin
Super User

Please share your macro, and describe how it fails.

 

For general approaches to exception handling in SAS, see lots of papers and books by Troy Hughes, e.g.: https://www.lexjansen.com/search/searchresults.php?q=troy%20hughes

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

The best way to return a value from a macro call is via a macro variable.  You need to make sure the macro variable exists in the calling environment (or the GLOBAL environment).

 

%macro mymacro(parm1,parm2,mvarname);
%if not %symexist(&mvarname) %then %global &mvarname;
....
%mend;

Then after you call the macro you can test the value of the macro variable.

%mymacro(parm1=sashelp.class,parm2=name,mvarname=status);
%if &status=1 %then %put It worked;
%else %put it failed;
ballardw
Super User

@hellohere wrote:

Here is a way to return a value inside a MACRO. 

 

But how to return a specific value, say -1, when the macro failed in middle?!

 

My macro simply reads a delimited file into a dataset in an automatic process; somehow it fails occasionally. I need know whether failed or not

and plan to re-read it seconds later if failed.  


Why does the read of a delimited file "fail"? Some concrete example might be in order. Fix the desired process instead of the error messaging.

 

My experience with "failed" reading of delimited data files always comes down to the accuracy of the description of the delimited file. Either someone changes the content of the file or one or more fields were not properly described and changing content brings out the flaw in the description.

hellohere
Pyrite | Level 9

For static file, it is. The macro reads in perfectly, but I need read it in real time in which the file is being written by C++. 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 893 views
  • 3 likes
  • 5 in conversation