5

I've got a macro that I've created on my local machine in a .sas file. I've also got a local dataset that have been using to test the macro. This dataset has the same descriptors as a remote dataset, but just less observations. Now, I'm trying to run my local macro against the remote dataset. Here is basically what I have:

This works as expected:

%include "C:\my_sas_macro.sas";

%my_sas_macro(my_data=work.localdata)

but then this generates an error (error follows):

%include "C:\my_sas_macro.sas";

rsubmit;
%my_sas_macro(my_data=remotelib.remotedata)
endrsubmit;

The log with error:

125  %include "C:\my_sas_macro.sas";
136
137  rsubmit;
NOTE: Remote submit to REMOTEID.__7551 commencing.
WARNING: Apparent invocation of macro MY_SAS_MACRO not resolved.
83   %my_sas_macro(my_data=remotelib.remotedata)
     -
     180
ERROR 180-322: Statement is not valid or it is used out of proper order.
84   endrsubmit;

NOTE: Remote submit to REMOTEID.__7551 complete.

I'm pretty sure I need to somehow transfer the %macro/%mend block over to the server, but I can't figure out how. I've seen the %SYSLPUT but that is for macro variables and not full macros.

Is there anyway that I can run my macro on the server without having to just SSH over the code and %include it there?

Thanks!

[Edit] Implemented Solution

So based on @CarolinaJay65 's answer I came up with the following macro which is working pretty well for me so far.

%macro include_on_server(file=);
%let server_file = ~/temp.sas;
%SYSLPUT macro_file=&file;
%SYSLPUT server_file = &server_file;

rsubmit;

proc upload
    infile= "&macro_file."
    outfile= "&server_file."
; run;

%include "&server_file.";
endrsubmit;

%mend include_on_server;

This allows me to just call %include_on_server(file="C:\my_file.sas") and then it's now included in my remote session.

Matt Klein
  • 7,856
  • 6
  • 45
  • 46

3 Answers3

6

How about using Proc Upload

 rsubmit;
 Proc Upload
  infile='C:\my_sas_macro.sas'
  outfile='//server/my_sas_macro.sas' ;
 run;

 %include '//server/my_sas_macro.sas';
 %my_sas_macro(my_data=remotelib.remotedata)

or you could assign the entire macro to a macro variable then use %syslput

Matt Klein
  • 7,856
  • 6
  • 45
  • 46
Jay Corbett
  • 28,091
  • 21
  • 57
  • 74
  • Thanks, I'll check out `Proc Upload;` For completeness, would you be able to show the correct way to "assign the entire macro to a macro variable"? – Matt Klein Sep 24 '12 at 01:05
  • Thanks, this works for me, but it's kind of a PITA because I have to manage the directory structure on the server (it won't automatically create directories that don't exist). I'll keep this as the accecpted answer unless some other solution comes along that doesn't require me to manage files on the server side. – Matt Klein Sep 24 '12 at 01:47
  • 1
    I'd add endrsubmit; at the end to make it clear that all is executed remotely. – vasja Sep 24 '12 at 10:38
  • 1
    You don't need to specify a server directory name. If you just give a name without the direcory on your `OUTFILE` statement, the file will be created in your remote home directory. – BellevueBob Sep 24 '12 at 13:59
  • Thanks @Bob, that was similar to what I figured out... At first I wanted to copy the file structure exactly as-is, but then realized that I can just copy to a single temp file, `%include` it, and then be done with it. I'll post my final working macro to show others what worked for me. – Matt Klein Sep 24 '12 at 16:33
  • 1
    @MattKlein When I want to save tempfiles I like to save them to the same folder as the `work` libname so they get cleaned up when the session is done. You can do this with `%sysfunc(pathname(work))`. – Robert Penridge Oct 01 '12 at 23:56
1

You have a few options, one of which being CarolinaJay's suggestion. Another is if you don't mind doing the processing locally, you can use a LIBNAME to refer to the server library:

libname server= slibref=; so libname myserver server=unix slibref=work;

if your server is named 'unix' in the connect script and you want its work directory.

PROC CATALOG, or I think also PROC COPY, can be used in conjunction with a server libref like above to copy the code, either as a macro or simply as source code. Macros are stored in a catalog (SASMACR), which can be copied from one libref to another (in this case from one machine to another). You could also store the source to create the macro in a catalog and move that, preventing you from having to compile the macro. This does require the catalogs to be compatible, though, which depends on the type of OS you are running on the server versus your local machine.

Finally, you can follow the suggestions in this paper: http://www.nesug.org/proceedings/nesug05/io/io2.pdf which combines the catalog solution and PROC UPLOAD together to allow even non-compatible machines to work together.

Joe
  • 62,789
  • 6
  • 49
  • 67
  • +1 Thanks for the great details! Unfortunatley for me, donig local processing is not an option since I need to work with 12 datasets that are around 6GB each. – Matt Klein Sep 24 '12 at 16:31
  • The PROC CATALOG solution will work fine, then - only the initial part of the suggestion was for local processing (see the PDF). – Joe Sep 24 '12 at 16:34
1

Put an rsubmit/endrsubmit inside the include, around the macro definition. When you include the local file, it will the complile the macro remotely.

EDIT:

i.e.

c:\remote_macro.sas contains the below :

rsubmit ;
  %MACRO MYMACRO ;
    /* do stuff */
  %MEND ;

  %MACRO ANOTHERMACRO(PARAM) ;
    /* Do other things */
    %PUT &PARAM ;
  %MEND ;
endrsubmit ;

Then run the following in SAS :

%inc "c:\remote_macro.sas" ;

rsubmit ;
  %MYMACRO ;
  %ANOTHERMACRO(Hello World) ;
endrsubmit ;
Chris J
  • 7,549
  • 2
  • 25
  • 25
  • This doesn't work =( The code in the rsubmit/endrsubmit block ends up being executed in the context of the server, which then gives the error: `WARNING: Physical file does not exist...` and then `ERROR: Cannot open %INCLUDE file C:\...`. – Matt Klein Sep 24 '12 at 16:29
  • See edit with example code. I think you're putting the rsubmit around the %inc, not around the macro within the %inc. – Chris J Sep 25 '12 at 12:36
  • Thanks! That works because you have the `rsubmit;`/`endrsubmit;` blocks in your macro file which means you can't include it locally, since it will always remote submit it. I was looking for a solution that would allow me to be able to include a file both locally and remotely. – Matt Klein Sep 25 '12 at 19:57