Link to home
Start Free TrialLog in
Avatar of aruku
aruku

asked on

%do loop - Remote sessions in SAS

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.
Avatar of bradanelson
bradanelson
Flag of United States of America image

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
Avatar of aruku
aruku

ASKER



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

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?
Avatar of aruku

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of theartfuldazzler
theartfuldazzler
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
Avatar of aruku

ASKER

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;
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.
Avatar of aruku

ASKER

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.

I sounds like you've thought this through and you've probably tested a few different designs.  Good luck!
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;