DATA Step, Macro, Functions and more

Call a sas program with run time parameters

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Call a sas program with run time parameters

I have a sas program named test.sas having a macro:

%macro mymacro(input1=, input2=, input3=);
/*
piece of code.

*/
%mymacro(input1=a, input2=b, input3=c)

I want to supply these macro input parameters as runtime parameters on unix server.

something like :

sas test.sas parameter1 parameter2 parameter3.

 

How will I call this program by passing input and get these inputs in my macro.

 

Please suggest a way.


Accepted Solutions
Solution
‎08-31-2017 09:07 AM
Super User
Posts: 7,795

Re: Call a sas program with run time parameters


Arjumand wrote:
Hi Kurt,

Can I get parameters in macro call:
%test(
a= %sysget(PARAM1)
,b= %sysget(PARAM2)
,c= %sysget(PARAM3)
,d= %sysget(PARAM4)
,e= %sysget(PARAM5)
);

Of course you can!

Based on your example (please use a code posting window - {i} or "little running man" in the future for posting code, to preserve formatting) I wrote a short program and saved it as envtest.sas:

%macro test(a=,b=,c=,d=,e=);
%put a=&a;
%put b=&b;
%put c=&c;
%put d=&d;
%put e=&e;
%mend;

%test(
a= %sysget(PARAM1)
,b= %sysget(PARAM2)
,c= %sysget(PARAM3)
,d= %sysget(PARAM4)
,e= %sysget(PARAM5)
);

Then I wrote this shell script:

export PARAM1=xxx
export PARAM2=yyy
export PARAM3=zzz
export PARAM4=111
export PARAM5=222
sas envtest.sas

After running the shell script, this is the log:

1          %macro test(a=,b=,c=,d=,e=);
2          %put a=&a;
3          %put b=&b;
4          %put c=&c;
5          %put d=&d;
6          %put e=&e;
7          %mend;
8
9          %test(
10         a= %sysget(PARAM1)
11         ,b= %sysget(PARAM2)
12         ,c= %sysget(PARAM3)
13         ,d= %sysget(PARAM4)
14         ,e= %sysget(PARAM5)
15         );
a=xxx
b=yyy
c=zzz
d=111
e=222
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: Das SAS System used:
      real time           0.28 seconds
      cpu time            0.02 seconds
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super Contributor
Posts: 440

Re: Call a sas program with run time parameters

You need to wrap your macro in a .sas file and use the -SYSPARM option to pass the parameters http://support.sas.com/documentation/cdl/en/hostwin/69955/HTML/default/viewer.htm#n0u0t8c3b3syeen1l1...

Contributor
Posts: 21

Re: Call a sas program with run time parameters

Posted in reply to ChrisBrooks
ok.. How will I retrieve the parameters in macro @ChrisBrooks
Super Contributor
Posts: 440

Re: Call a sas program with run time parameters

There's a corresponding SYSPARM() function http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n1ug7uhp2d4... which will retrieve the parameters in one string which you can then split up with the SCAN() function.

Super User
Posts: 7,795

Re: Call a sas program with run time parameters

Use a shell script that sets environment variables before running the program:

export PARAM1=xxx
export PARAM2=yyyy
export PARAM3=zzz
sas test.sas

And then retrieve the environment variables in your SAS code

%let param1=%sysget(PARAM1);
%let param2=%sysget(PARAM2);
%let param3=%sysget(PARAM3);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: Call a sas program with run time parameters

Posted in reply to KurtBremser
Hi Kurt,

Can I get parameters in macro call:
%test(
a= %sysget(PARAM1)
,b= %sysget(PARAM2)
,c= %sysget(PARAM3)
,d= %sysget(PARAM4)
,e= %sysget(PARAM5)
);
Solution
‎08-31-2017 09:07 AM
Super User
Posts: 7,795

Re: Call a sas program with run time parameters


Arjumand wrote:
Hi Kurt,

Can I get parameters in macro call:
%test(
a= %sysget(PARAM1)
,b= %sysget(PARAM2)
,c= %sysget(PARAM3)
,d= %sysget(PARAM4)
,e= %sysget(PARAM5)
);

Of course you can!

Based on your example (please use a code posting window - {i} or "little running man" in the future for posting code, to preserve formatting) I wrote a short program and saved it as envtest.sas:

%macro test(a=,b=,c=,d=,e=);
%put a=&a;
%put b=&b;
%put c=&c;
%put d=&d;
%put e=&e;
%mend;

%test(
a= %sysget(PARAM1)
,b= %sysget(PARAM2)
,c= %sysget(PARAM3)
,d= %sysget(PARAM4)
,e= %sysget(PARAM5)
);

Then I wrote this shell script:

export PARAM1=xxx
export PARAM2=yyy
export PARAM3=zzz
export PARAM4=111
export PARAM5=222
sas envtest.sas

After running the shell script, this is the log:

1          %macro test(a=,b=,c=,d=,e=);
2          %put a=&a;
3          %put b=&b;
4          %put c=&c;
5          %put d=&d;
6          %put e=&e;
7          %mend;
8
9          %test(
10         a= %sysget(PARAM1)
11         ,b= %sysget(PARAM2)
12         ,c= %sysget(PARAM3)
13         ,d= %sysget(PARAM4)
14         ,e= %sysget(PARAM5)
15         );
a=xxx
b=yyy
c=zzz
d=111
e=222
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: Das SAS System used:
      real time           0.28 seconds
      cpu time            0.02 seconds
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: Call a sas program with run time parameters

Posted in reply to KurtBremser
@KurtBremser: Just one last question, how can we call the same program from command promt in windows?
Thanks so much for your help Smiley Happy
Super User
Posts: 7,795

Re: Call a sas program with run time parameters

To set environment variables in Windows from the commandline, use the set command.

My method would be to write a simple .bat file that looks quite similar to the UNIX shell script, with set instead of export.

Others may prefer to use VBS, as that is more "modern".

Since I don't run SAS on Windows, the question does not arise for me. I have a real operating system at hand.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,028

Re: Call a sas program with run time parameters

Yes. You can do it .@Rick_SAS has written a blog about it before. Check out.

 

http://blogs.sas.com/content/iml/2015/03/16/pass-params-sysget.html

Contributor
Posts: 21

Re: Call a sas program with run time parameters

Thanks a lot everyone.
Contributor
Posts: 21

Re: Call a sas program with run time parameters

Hi @Ksharp,

I am running as per you suggested and the program seems to be executing, but the parameters are not read using the command in this blog. Plus I am getting "-set is not recognized as an internal or external command" error in console. Please suggest.
Super User
Posts: 10,028

Re: Call a sas program with run time parameters

Your sas is under UNIX/LINUX/AIX OS? If it was,then Kurt's answer is right for you. otherwise, I don' know what is going on with it.

calling @Rick_SAS

Regular Contributor
Posts: 227

Re: Call a sas program with run time parameters

see my paper Sysparm Companion

 

code is on this page: sco Parse sysparm

 

Ron Fehd  command-line options maven

 

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 255 views
  • 2 likes
  • 5 in conversation