BookmarkSubscribe
Choose Language Hide Translation Bar
poulravn
Community Trekker

DeclareMacros option of SAS Submit: Failing to transfer correctly?

Hi, I am trying to apply the DeclareMacros option of SAS Submit. It is supposed to allow transfer of variable values to macros in SAS job. I made this tiny snip (it will run though, but failing):

 

a = "test1";
b = "test2";

Meta Connect( "sas.radiometer.dk", 8561, "", ProfileLookup );
_importsasconn = SAS Connect( "SASApp" );

SAS Submit("
data test;
x = &a;
y = &b;
run;
 ",
 declaremacros(a,b),GetSASLog(true));
 

SAS log says:

768        %let a=%NRBQUOTE(test1);
769        %let b=%NRBQUOTE(test2);
770        
771        data test;
772        x = &a;
773        y = &b;
774        run;

NOTE: Variable test1 is uninitialized.
NOTE: Variable test2 is uninitialized.
NOTE: The data set WORK.TEST has 1 observations and 4 variables.

and the data set test contains:

 

declaremacros_result.jpg

 

I fail to see, what I am doing wrong.

 

Best regards

Poul Ravn Sørensen

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

I should also point out that the examples that you've posted don't really need the power of macro variables on the SAS side. Rather, using Eval Insert() to build your SAS_sbmt_str may be useful if your real case is similarly simple.

 

a = "test1";
b = "test2";
c = "'15may2019 00:00'dt";

SAS_sbmt_str = Eval Insert( "data test;
x = '^a^';
y = '^b^';
d =  ^c^;
run;
proc print; run;" );

SAS Submit( SAS_sbmt_str, GetSASLog( true ) );

 

-Jeff
5 REPLIES 5
Jeff_Perkinson
Community Manager Community Manager

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

You're using the macro variables incorrectly.

 

If you want to use them on the right side of an assignment in the data step you'll need to quote them.

 

a = "test1";
b = "test2";

Meta Connect( "sas.radiometer.dk", 8561, "", ProfileLookup );
_importsasconn = SAS Connect( "SASApp" );

SAS Submit("
data test;
x = \!"&a\!";
y = \!"&b\!";
run;

proc print;
run;
 ",
 declaremacros(a,b),GetSASLog(true));

Check Using Macro Variables for more information. 

 

-Jeff
poulravn
Community Trekker

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

Hi again, what Jeff suggested works. Thanks.

I am still grappling though with datetime variables. See code. Question is: How do I get d_num assigned the value of the datetime variable?  (Rearragend a bit but it is still the same code, only the submit string is built before the SAS Submit statement.)

a = "test1";
b = "test2";
c = "'15may2019 00:00'dt";

Meta Connect( "sas.radiometer.dk", 8561, "", ProfileLookup );
_importsasconn = SAS Connect( "SASApp" );
SAS_sbmt_str = 
"data test;
x = \!"&a\!";
y = \!"&b\!";
d = \!"&c\!";
d_num = ???;
run;
proc print; run;";
SAS Submit(SAS_sbmt_str, declaremacros(a,b,c),GetSASLog(true));

Best regards Poul R S

0 Kudos
Jeff_Perkinson
Community Manager Community Manager

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

Dealing with SAS Macro variables and their resolution can be confusing, especially when it comes to quoting macro variables that have quotes in them. There's a whole section of the SAS documentation on Macro Quoting.

 

Let me begin by saying that I know enough about this topic to get you started and give you something that works, but not enough to explain all the details. You should probably start frequenting the SAS Programming board in the SAS Support Communities to get a more thorough explanation and undersanding.

 

It would be best for you to figure out what you need in SAS alone and then figure out how to get JSL to pass that along to SAS.

 

Here's the SAS syntax for what you're trying to do.

 

%let a = test1;
%let b = test2;
%let c = '15may2019 00:00'dt;

data test;
x = "&a";
y = "&b";
d = &c;
run;
proc print; run;

Notice that &a and &b need to be in double quotes (macro variables only resolve in double quotes) because you want to use their values as string literals and they don't have quotes in their values.

 

On the other hand, you've put your datetime value as a datetime constant with single quotes and the dt to indicate that it's a datetime constant. Since that macro value already has single quotes in it you don't need to put it in quotes on the right side of an assignment.

 

So far, so good, but there's a wrinkle. To get the value of your JSL variables into macro variables JMP uses the %NRBQUOTE function to define them. If you look at the SAS log from a SAS Submit(..., Declare Macros(...)) you'll see that:

 

103        %let a=%NRBQUOTE(test1);
104        %let b=%NRBQUOTE(test2);
105        %let c=%NRBQUOTE('15may2019 00:00'dt);

 

This is because JMP doesn't know whether your JSL variable contains any of the special characters (' " ( ) + − * / < > = ¬ ^ ~ ; , # blank AND OR NOT EQ NE LE LT GE GT IN) that need to be masked in SAS.

 

If we do the same thing in SAS and submit this code:

 

%let a=%NRBQUOTE(test1);
%let b=%NRBQUOTE(test2);
%let c=%NRBQUOTE('15may2019 00:00'dt);

data test;
x = "&a";
y = "&b";
d = &c;
run;
proc print; run;

 

we'll see this in the SAS Log:

 

150        %let a=%NRBQUOTE(test1);
151        %let b=%NRBQUOTE(test2);
152        %let c=%NRBQUOTE('15may2019 00:00'dt);
153        
154        data test;
155        x = "&a";
156        y = "&b";
157        d = &c;
NOTE: Line generated by the macro variable "C".
157         '15may2019 00:00'dt
            _
            386
             _
             76
ERROR 386-185: Expecting an arithmetic expression.

ERROR 76-322: Syntax error, statement will be ignored.

158        run;

NOTE: The SAS System stopped processing this step because of errors.

 

[This is the point where my understanding breaks down. I can tell you what you need to do to use a macro variable with masked characters but no more about why you need to do that. Again, head over to the SAS Programming board to get more information.]

 

So, because the SAS macro variable is defined by JMP using the %NRBQUOTE function, and, because we don't want the single quotes masked we need to use the %UNQUOTE function to get them.

 

So, in JSL it looks like this:

 

a = "test1";
b = "test2";
c = "'15may2019 00:00'dt";

SAS_sbmt_str =
"data test;
x = \!"&a\!";
y = \!"&b\!";
d =  %UNQUOTE(&c);
run;
proc print; run;";
SAS Submit( SAS_sbmt_str, declaremacros( a, b, c ), GetSASLog( true ) );

Others may want to know what more you learn about this issue in your exploration and use of the SAS Support Communities. So, post any links to discussion threads that you find useful.

 

-Jeff
Jeff_Perkinson
Community Manager Community Manager

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

I should also point out that the examples that you've posted don't really need the power of macro variables on the SAS side. Rather, using Eval Insert() to build your SAS_sbmt_str may be useful if your real case is similarly simple.

 

a = "test1";
b = "test2";
c = "'15may2019 00:00'dt";

SAS_sbmt_str = Eval Insert( "data test;
x = '^a^';
y = '^b^';
d =  ^c^;
run;
proc print; run;" );

SAS Submit( SAS_sbmt_str, GetSASLog( true ) );

 

-Jeff
poulravn
Community Trekker

Re: DeclareMacros option of SAS Submit: Failing to transfer correctly?

Thanks for explaining and providing an alternative solution, using 'Eval Insert' of JSL. I think for my purpose that it will be enough to use the latter solution, since all I want to accomplish is to transfer a few user choices to the SAS Code. And, one does not need the DeclareMacros call at all. It works without that.

 

Best regards

Poul Ravn Sørensen

0 Kudos