DATA Step, Macro, Functions and more

%nrstr macro call execute issue with macro variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

%nrstr macro call execute issue with macro variables

HI

Hope you are well

I am using the below code to run a series of macros if run='Y'

%macro pgrun(macro,run);

%if %upcase("&run") eq "Y"  %then %&macro; ;

%mend pgrun;

data _null_;

set driver;

if macro ne '';

call execute('%nrstr(%pgrun) (' !! macro !! ' , ' !! run !! ' );');

run;

As above I am using the %nrstr option so any macros within macros are compiied correctly however I have found  with the %nstr if I call a macro with a macro call within it can alter the text

e.g.  One macro call is processed as below:

%pgrun (LABCONNECT((6-2-2014 102404 AM ) -13-0103 XFER) , Y );

but this should be  %pgrun (LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER) , Y ) which is processed if the %nrstr is removed i.e. the space after the '-' has been removed. '(6-2-2014 102404 AM ) - 13-0103 XFER' is the name of a text file I wish to import

Can anyone help prevent this occurrence without having not to use the %nrstr option or simply amending the text file name so it is compatible

Many thanks in advance

Ben


Accepted Solutions
Solution
‎06-04-2014 01:33 PM
Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

Please %nrbquote instead of %nrstr.  Looks like this has been resolved in 9.4

%macro foo(macro , run);

   %if &run=Y %then %&macro;

%mend;

%macro labconnect(x);

   %put &sysmacroname &x;

%mend;

data _null_;

   macro = 'LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER)';

   run   = 'Y';

   call execute( '%nrbquote(%foo( ' !! macro !! ' , ' !! run !! ' ))' );

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,076

Re: %nrstr macro call execute issue with macro variables

Looks like the macro processing is modifying the string you are trying to pass.

Probably the easiest fix is modify the LABCONNECT macro to expect the filename to be enclosed in quotes so that you blanks are preserved as is.

%macro labconnect(filename.....);

  ....

  infile &filename ....

....

%mend ;

Then generate the macro call as :

%pgrun (LABCONNECT("(6-2-2014 102404 AM ) - 13-0103 XFER)" , Y ))


Personally I find it much easier to use PUT statements to write the generated code myself rather than using CALL EXECUTE. It is also much easier to debug as you can run the data step that generates the code and then examine the generated code to make sure that the syntax is correct.  Also if you did this then your could eliminate the need for the PGMRUN macro and just put the test of the RUN parameter into the code generating data step.


Occasional Contributor
Posts: 6

Re: %nrstr macro call execute issue with macro variables

Hi Tom

Many thanks for the reply

below is my code for reading the file in

%macro labconnect(infile);

data sas.source;

infile "&topdirlg/sas/&infile..txt" dsd dlm='|' lrecl=32767 missover  firstobs=2;

input

STUDYID: $15. DOMAIN: $2. USUBJID: $15. DOB: $15. SEX: $1. LBSEQ: $5. LBGRPID: $5. LBREFID: $15. LBSPID: $8.

LBTESTCD: $50. LBTEST: $50. LBCAT: $50.  LBSCAT: $50.  LBORRES: $200.

LBORRESU: $50. LBORNRLO: $50. LBORNRHI: $50.

LBSTRESC: $200. LBSTRESN: $50. LBSTRESU: $50. LBSTNRLO: $50. LBSTNRHI: $50. LBSTNRC: $50.

LBNRIND: $15. LBSTAT: $50. LBREASND: $200. LBNAM: $50. LBLOINC: $50. LBSPEC: $50. LBSPCCND: $50.

LBMETHOD: $50. LBBLFL: $50. LBFAST: $1. LBDRVFL: $50.

LBTOX: $50. LBTOXGR: $50. VISITNUM: $50. VISIT: $50. VISITDY: $50. LBDTC: $50. LBENDTC: $50. LBDY: $50. LBTPT: $50.

LBTPTNUM: $50. LBELTM: $50. LBTPTREF: $50. LBRFTDTC: $50. COMMENT: $200.;

run;

Using call execute with ""s though causes issues with options and the call execute:

E 155-205: Line generated by the CALL EXECUTE routine.

4     + data sas.source; infile "S:\Lab_Data\NGM130103\Labconnect/sas/"(6-2-2014 102404 AM ) -13-0103 XFER".txt" dsd dlm='|' lrecl=32767

                                                                       -

                                                                       23

4    !+missover  firstobs=2; input  STUDYID: $15. DOMAIN: $2. USUBJID: $15. DOB: $15. SEX: $1. LBSEQ: $5. LBGRPID: $5. LBREFID: $15.

ERROR 23-2: Invalid option name (.

Super User
Super User
Posts: 7,076

Re: %nrstr macro call execute issue with macro variables

Here is one way to allow passing part of physical path as a quoted string.

You can use the DEQUOTE() function to remove the outer quotes so that you can add the prefix and suffix values.


%macro labconnect(infile);

%let infile = "&topdirlg/sas/%sysfunc(dequote(&infile)).txt" ;

data sas.source;

  infile &infile dsd dlm='|' lrecl=32767 missover  firstobs=2;

...

Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

It's not the way I would go about designing it, but what you are trying to do should work fine.  You problem must be someplace else in your process.  %nrstr would definitely not be responsible for deleting a space there.

%macro foo(macro , run);

   %if &run=Y %then %&macro;

%mend;

%macro labconnect(x);

   %put &sysmacroname &=x;

%mend;

data _null_;

   macro = 'LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER)';

   run   = 'Y';

   call execute( '%nrstr(%foo)( ' !! macro !! ' , ' !! run !! ' )' );

run;

NOTE: CALL EXECUTE generated line.

1         + %foo( LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER) , Y )

LABCONNECT X=(6-2-2014 102404 AM ) - 13-0103 XFER

Occasional Contributor
Posts: 6

Re: %nrstr macro call execute issue with macro variables

Many thanks for the reply

It is a real odd one as not all blanks are ignored and literally If I remove the %nstr the program work fine. I can only assume the macro compling is doing something strange here using %nrstr

Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

You will have to post the contents of your called macro then.  I would also confirm that the presumed space is not a special hex character other than an actual space.

Occasional Contributor
Posts: 6

Re: %nrstr macro call execute issue with macro variables

HI

Thanks again for your help

I read the data for the programs from a microscoft excel work sheet. In regard to the space I literally cut and pasted the file name from explorer into the excel sheet. I've tried copying the file and renaming manually and still the same issue with %NRSTR

Line 356: ERROR: Physical file does not exist, S:\Lab_Data\NGM130103\Labconnect\sas\(6-2-2014 10

:          2404 AM ) -13-0103 XFER - Copy.txt.

But without %nrstr and same inputted data

NOTE: 63 records were read from the infile "S:\Lab_Data\NGM130103\Labconnect/sas/(6-2-2014 102404 AM ) - 13-0103 XFER - Copy.txt".

      The minimum record length was 141.

      The maximum record length was 969.

I'm using SAS 9.2.

I think I'm going to bite the bullet and rename the source file....

Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

What happens if you run exactly what I posted.

Occasional Contributor
Posts: 6

Re: %nrstr macro call execute issue with macro variables

Thanks again but looks like it's definitely something buggy with my 9.2 as the mystery space has again disappeared

1    %macro foo(macro , run);

2       %if &run=Y %then %&macro;

3    %mend;

4

5

6    %macro labconnect(x);

7       %put &sysmacroname &=x;

8    %mend;

9

10

11   data _null_;

12      macro = 'LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER)';

13      run   = 'Y';

14

15

16      call execute( '%nrstr(%foo)( ' !! macro !! ' , ' !! run !! ' )' );

17   run;

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

NOTE: CALL EXECUTE generated line.

1   +  %foo( LABCONNECT((6-2-2014 102404 AM ) -13-0103 XFER) , Y )

LABCONNECT &=x

Solution
‎06-04-2014 01:33 PM
Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

Please %nrbquote instead of %nrstr.  Looks like this has been resolved in 9.4

%macro foo(macro , run);

   %if &run=Y %then %&macro;

%mend;

%macro labconnect(x);

   %put &sysmacroname &x;

%mend;

data _null_;

   macro = 'LABCONNECT((6-2-2014 102404 AM ) - 13-0103 XFER)';

   run   = 'Y';

   call execute( '%nrbquote(%foo( ' !! macro !! ' , ' !! run !! ' ))' );

run;

Occasional Contributor
Posts: 6

Re: %nrstr macro call execute issue with macro variables

Many thanks again for all your help Tom and

I think it may be time to upgrade my SAS and many thanks for your suggestions have a great rest of the week

Ben

Trusted Advisor
Posts: 1,301

Re: %nrstr macro call execute issue with macro variables

Yes, given your log you will need both recommendations from myself and Tom to fix all of your issues.

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 331 views
  • 3 likes
  • 3 in conversation