?
Solved

%do loop - Remote sessions in SAS

Posted on 2011-05-11
12
Medium Priority
?
1,833 Views
Last Modified: 2013-11-16
Hi all,
I am using SAS Connect to connect to multiple sessions at the same time and run pieces of code to reduce the execution time.

Sample code:
************************************************************************Start
signon a1 sascmd = '/sasadmin/9.2/sas92home/sas' ;

%let flag1 = 1;
%let flag2 = 2;
%let totcnt = 2;
%let username = a1;
%syslput flag1 = &flag1;
%syslput flag2 = &flag2;
%syslput totcnt = &totcnt;

%macro one;

killtask a1;

rsubmit a1 wait = NO;

libname xxx '/sas/xxxx/xxxxxxx';

data xxx.one48;
set xxx.A_date;

%do i = 1 %to &totcnt;
f&i. = &&flag&i.;
%end;

run;

endrsubmit;

%mend one;

%one;

signoff a1;

*************************************************** End;

but am not able to resolve the 'i' in the loop.

Log:

*********************************Start


NOTE: Background remote submit to A1 in progress.
33        
34         signoff a1;
NOTE: Remote submit to A1 commencing.
1     libname xxxxxx '/sas/xxxxx/xxxxxx';
NOTE: Libref xxxxwas successfully assigned as follows:
      Engine:        V9
      Physical Name: libname xxxxxx '/sas/xxxxx/xxxxxx';
2     data xxxx.one48;
3     set xxxx.A_DATE_B38;
4     f&i. = &&flag&i.;
      -
      180
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference FLAG not resolved.
WARNING: Apparent symbolic reference I not resolved.
5     f&i. = &&flag&i.;
      -
      180
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference FLAG not resolved.
WARNING: Apparent symbolic reference I not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.

Can anyone please help me on this.

Appreciate your help. Thanks.
0
Comment
Question by:aruku
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 9

Expert Comment

by:bradanelson
ID: 35740490
do loops inside of a datastep do not need the % signs.  Do loops outside of a datastep, in a macro does use the % signs.

Here is a link that describes the differences.
http://www.lexjansen.com/pnwsug/2009/Fehd,%20Ron%20-%20Do%20Which%20Loop,%20Until%20or%20While.pdf
0
 

Author Comment

by:aruku
ID: 35740878


thanks for the fast response. I did change it to regular 'do' but the 'i' is still not getting resolved in the log.

0
 
LVL 7

Expert Comment

by:d507201
ID: 35741560
Remember that macro variables are character.  If totcnt=10 then %DO 1 %TO &TOTCNT; resolves to DO 1 to '10'; and quite rightly it throws an error even though the message is a bit misleading.  To fix it you need to change TOTCNT to numeric, specifically an integer value.

The %EVAL function does just this.  Change your code to
     %DO 1 %TO %EVAL(&TOTCNT-0);

I'm thinking you need a macro do-loop so that the value of i can be used in the variable naming, right?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:aruku
ID: 35741921
I tried the way you have mentioned and am getting the same error. When I trigger the same code without remote session. I am able to resolve the 'i' to get the necessary values.

Yes, I am using the value of i in the variable naming.
0
 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 2000 total points
ID: 35744154
Hi

To make sure that the macro %DO loop resolves on the remote session, enclose the entire %do loop in a %NRSTR() function.  This function "hides" a number of special characters, including & and % from the macro processor.  You need to create another macro (two) on the remote session to enable the %do loop.

%macro one;

killtask a1;

rsubmit a1 wait = NO;

libname xxx '/sas/xxxx/xxxxxxx';

data xxx.one48;
set xxx.A_date;


%NRSTR(
%macro two;
%do i = 1 %to &totcnt;
f&i. = &&flag&i.;
%end;
%mend;
%two;);

run;

endrsubmit;

%mend one;

%one;
0
 
LVL 7

Expert Comment

by:d507201
ID: 35746742
Very curious.  I've never had to use %NRSTR to make this sort of thing work.

Are you sure that the initial values of your macro variables are being received by the remote session?  Immediately after the rsubmit command run %put _user_; to show the user-created macro variables.
0
 
LVL 11

Expert Comment

by:theartfuldazzler
ID: 35746885
Hi

I have to use it regularly for remote sessions.  The issues is SAS is not always good at understanding whether to run the macro in the local or remote session.  What is happening, is the %DO loop is running in the local session, but the f&i is resolving in the remote session.  Either you need to force resolve the f&i portion in the local session, or you need to force the %do loop to occur in the remote session.  I generally find the %NRSTR() solution the easier of the two.

D
0
 

Author Comment

by:aruku
ID: 35748399
Great, the do loop is getting resolved using the above logic. thanks so much. One more question. Pl bear with me.

I have a macro at the top of the sign on as total with parameters as below. I need to refer this parameter in the remote session as the name of the dataset (in bold).


How can i get it this way.

Really appreciate your help. Thanks.

%macro total (j);

signon a1 sascmd = '/sasadmin/9.2/sas92home/sas' ;

%let flag1 = 1;
%let flag2 = 2;
%let totcnt = 2;
%let username = a1;
%syslput flag1 = &flag1;
%syslput flag2 = &flag2;
%syslput totcnt = &totcnt;



%macro one;

killtask a1;

rsubmit a1 wait = NO;

libname xxx '/sas/xxxx/xxxxxxx';

data xxx.one&j.;
set xxx.A_date;


%NRSTR(
%macro two;
%do i = 1 %to &totcnt;
f&i. = &&flag&i.;
%end;
%mend;
%two;);

run;

endrsubmit;

%mend one;

%one;
0
 
LVL 7

Expert Comment

by:d507201
ID: 35748684
Artful, very interesting.  I've never written a macro that initiates a remote session so I've never encountered the problem.  I'll remember your solution.

Aruku, looking at your code, I don't see that is is necessary that the remote session be initiated inside the macro.  Initiating it outside the macro would let the macro be compiled in the same environment as it executes.
0
 

Author Comment

by:aruku
ID: 35749460
d507201, I will be not be able to include the sigon above the macro as below is the scenario:

My code structure has about 4 macros

%macro1 (j,k,l);

%macro2(j,k,l);

%macro3(j,k,l);

%macro4(j,k,l);


Now macro 1 and Macro 2 should run sequentially and am now trying to run 3 , 4 parallely.

I have a final macro as macro5 which is executed which paraterizes the selection of above macros



%macro5;

%if param = 1 %then

%macro1(j = 1, k=2, l = 3); ; %macro2(j = 1, k=2, l = 3); %macro3(j = 1, k=2, l = 3); %macro4(j = 1, k=2, l = 3);

%if param = 2 %then

%macro1; %macro2; %macro3;

%if param = 3 %then

%macro1; %macro2; %macro4;

%mend macro5;


So considering the above structure I need to trigger this macro in the client session:

but If I trigger it keeping the signon out. I feel this is not going to work.

Pl let me know if I am missing any thing here.

0
 
LVL 7

Expert Comment

by:d507201
ID: 35750813
I sounds like you've thought this through and you've probably tested a few different designs.  Good luck!
0
 
LVL 11

Expert Comment

by:theartfuldazzler
ID: 35752212
Hi Aruku

If I look at your code, it should work.  I would put a %SYSLPUT j = &j; after the signon.  That way, it doesn't really matter if the &j resolves in the remote or local session as they will be the same value.

If you want to resolve in the remote session, then you would just use the %NRSTR() function again (or just widen the function):

rsubmit a1 wait = NO;

libname xxx '/sas/xxxx/xxxxxxx';
%NRSTR(
data xxx.one&j.;
set xxx.A_date;



%macro two;
%do i = 1 %to &totcnt;
f&i. = &&flag&i.;
%end;
%mend;
%two;

run;
);
endrsubmit;

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question