Executing a dynamic command which is masked with NRSTR

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Executing a dynamic command which is masked with NRSTR

[ Edited ]

I'm sure I resolved this once before when dealing with URIs but I cannot for the life of me get this to work.

 

I don't like warnings, and the array of % symbols in this Unix command are causing about 15 warnings per loop.

 

Example code which is contained within a DO loop in a %MACRO:

data _null_;
ps = %nrstr("ssh -o StrictHostKeyChecking=no ""&ServerName"" 'find /logdir/ -type f -printf ""%p|%TY-%Tm-%Td %TX|%AY-%Am-%Ad %AX|%s|%u|%U\n"" > &projDir./Data/output.&ServerName'");
call system(ps);
;run;

 

When using NRSTR, the call system cannot resolve the entry "ps" which is created directly above it. I've also tried the same with a %let statement and then using call system(&ps.) but still, nothing.

 

BQUOTE, NRBQUOTE, STR, etc, etc do not remove warnings (and in some cases, BQUOTE particularly, I get errors).

 

Does anyone have a working example where they have been able to construct a command with "'s, 's %'s and other symbols and can then execute against it?

 

The closest I have got to a clean log is with NRSTR, but the output cannot be executed against.

 

 

Thanks in advance,

 

Dan

 

 


Accepted Solutions
Solution
‎01-11-2018 10:54 AM
Super User
Posts: 6,011

Re: Executing a dynamic command which is masked with NRSTR

[ Edited ]

I don't have a working example, but I do have a suggestion.

 

Note that single quotes suppress all macro activity, while double quotes permit macro activity (needed to allow &ServerName to resolve).

 

Here's an untested attempt at what you might be able to use to do this:

 

data _null_;

ps = cat("ssh -o StrictHostKeyChecking=no ""&ServerName"" 'find /logdir/ -type f -printf ",

         '"%p|%TY-%Tm-%Td %TX|%AY-%Am-%Ad %AX|%s|%u|%U\n"',

         " > &projDir./Data/output.&ServerName'");

 

It looks like @RW9 and I are thinking along similar lines.  You might need to see what the values are that get assigned to PS, and tweak the results (such as where the quotes go) since our suggestions are similar but not identical.

View solution in original post


All Replies
Super User
Super User
Posts: 8,634

Re: Executing a dynamic command which is masked with NRSTR

That code will not work as you are mixing datastep and macro code.  %nstr() is a macro itself.  With the data _null_ you already have the ability to process the data fully in a string:

data _null_;
  ps=cat('"ssh -o StrictHostKeyChecking=no ',"&ServerName",' find /logdir/ -type f -printf "%p|%TY-%Tm-%Td %TX|%AY-%Am-%Ad %AX|%s|%u|%U\n" > ',"&projDir.",'/Data/output.',"&ServerName",'"');
  call system(ps);
run;

Or something along those lines.  Basically I am just concatenating the various parts of the string together using Base SAS string functions to get it looking as I want.

Solution
‎01-11-2018 10:54 AM
Super User
Posts: 6,011

Re: Executing a dynamic command which is masked with NRSTR

[ Edited ]

I don't have a working example, but I do have a suggestion.

 

Note that single quotes suppress all macro activity, while double quotes permit macro activity (needed to allow &ServerName to resolve).

 

Here's an untested attempt at what you might be able to use to do this:

 

data _null_;

ps = cat("ssh -o StrictHostKeyChecking=no ""&ServerName"" 'find /logdir/ -type f -printf ",

         '"%p|%TY-%Tm-%Td %TX|%AY-%Am-%Ad %AX|%s|%u|%U\n"',

         " > &projDir./Data/output.&ServerName'");

 

It looks like @RW9 and I are thinking along similar lines.  You might need to see what the values are that get assigned to PS, and tweak the results (such as where the quotes go) since our suggestions are similar but not identical.

Occasional Contributor
Posts: 16

Re: Executing a dynamic command which is masked with NRSTR

[ Edited ]
Posted in reply to Astounding

Hi there, thanks for your responses

Single and double quoting isn't an unknown factor for me, so I'm happy with the understanding of how a macro variable will not be resolved within single quotes, which is what caused further issues because I could have wrapped the whole thing in single quotes and the issue would have gone away...

@Astounding has provided a working solution, as it concatenates a working string within single quotes without physically wrapping it in single quotes, therefore the macro variables resolve, and the %s are hidden, which is exactly what I needed.

Thanks again to both of you for your help.


Dan

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 100 views
  • 0 likes
  • 3 in conversation