DATA Step, Macro, Functions and more

Filename quote fun!

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 92
Accepted Solution

Filename quote fun!

Hello everyone

I'm trying to call cURL within SAS to access an api with oauth 1.0. In order to this, I need to construct an appropriate header.  Unfortunately, I cannot specify the header correctly in SAS because of the quotes.  I've read several posts here and on other sites about how to handle quotes within a macro variable. My code forms a macro variable with the necessary quotes.  The issue is when I put it in the filename statement.  I get an error.

%let consumer_key=%quote(")cheezwhiz%quote(");

%let header = "curl --header 'Authorization: OAuth oauth_consumer consumer_key=&consumer_key.'";

%put &header; gives "curl --header 'Authorization: OAuth oauth_consumer consumer_key="cheezwhiz"'" which is exactly what I want.

However, when I try to execute this within a filename statement, e.g.,  filename curl pipe &header.;, I get the following error message: ERROR 23-2: Invalid option name cheezwhiz.

Does anyone know of a way to circumvent this error?

Thanks very much for any suggestions.

-Bill


Accepted Solutions
Solution
‎12-07-2014 09:20 AM
Respected Advisor
Posts: 3,799

Re: Filename quote fun!

Posted in reply to BillJones

Try letting the quote function have all the fun.

%let consumer_key=cheezwhiz;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;


39         %let consumer_key=cheezwhiz;
40         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
40       ! consumer_key=%sysfunc(quote(&consumer_key))))));
41         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""""""
42        
43         filename curl pipe &header.;
44         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"""

View solution in original post


All Replies
Solution
‎12-07-2014 09:20 AM
Respected Advisor
Posts: 3,799

Re: Filename quote fun!

Posted in reply to BillJones

Try letting the quote function have all the fun.

%let consumer_key=cheezwhiz;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;


39         %let consumer_key=cheezwhiz;
40         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
40       ! consumer_key=%sysfunc(quote(&consumer_key))))));
41         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""""""
42        
43         filename curl pipe &header.;
44         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"""
Frequent Contributor
Posts: 92

Re: Filename quote fun!

Posted in reply to data_null__

data_null_,

Thanks so much for the code!  The quote function is definitely having all the fun now. Smiley Happy

Thanks again,

Bill

Frequent Contributor
Posts: 92

Re: Filename quote fun!

Posted in reply to data_null__

Quick follow-up question: How do I add additional quoted strings to the pipe statement.  For example, I tried to add a consumer_secret key with code below.

%let consumer_key=cheezwhiz;

%let consumer_secret=coorslight;

%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key)),consumer_secret=%sysfunc(quote(&consumer_secret))))));

%put NOTE: %superq(header);

filename curl pipe &header.;

filename curl list;

The code runs, but I cannot see the second key (consumer_secret) in the log.  What's the best way to deal with this issue?  Should I use the catx function?  Or is there a more direct solution?

-Bill

Respected Advisor
Posts: 3,799

Re: Filename quote fun!

Posted in reply to BillJones

It's the comma.  Do you need it?

Frequent Contributor
Posts: 92

Re: Filename quote fun!

Posted in reply to data_null__

Thanks for the response and yes I need the commas.  They're required for the OAuth header.

Respected Advisor
Posts: 3,799

Re: Filename quote fun!

Posted in reply to BillJones

Put the comma in %NRSTR(,)   %STR might work too.

%let consumer_key=cheezwhiz;
%let consumer_secret=coorslight;
%let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer consumer_key=%sysfunc(quote(&consumer_key))%nrstr(,)consumer_secret=%sysfunc(quote(&consumer_secret))))));
%put NOTE: %superq(header);

filename curl pipe &header.;
filename curl list;



32         %let consumer_key=cheezwhiz;
33         %let consumer_secret=coorslight;
34         %let header = %sysfunc(quote(curl --header %sysfunc(quote(Authorization: OAuth oauth_consumer
34       ! consumer_key=%sysfunc(quote(&consumer_key))%nrstr(,)consumer_secret=%sysfunc(quote(&consumer_secret))))));
35         %put NOTE: %superq(header);
NOTE:
"curl --header ""Authorization: OAuth oauth_consumer consumer_key=""""cheezwhiz"""",consumer_secret=""""coorslight"""""""
36        
37         filename curl pipe &header.;
38         filename curl list;
NOTE: Fileref= CURL
      Physical Name= curl --header
"Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"",consumer_secret=""coorslight"""
Frequent Contributor
Posts: 92

Re: Filename quote fun!

Posted in reply to data_null__

data _null_,

Thanks very much for the additional code!

-Bill

Trusted Advisor
Posts: 1,301

Re: Filename quote fun!

Posted in reply to BillJones

There are plenty of method available in SAS to execute these http calls without having to use an external tool, such as cURL. 

%let consumer_key=cheezwhiz;

%let consumer_secret=coorslight;

filename headIn temp;

data _null_;

file headIn;

array oauth[2] $ 200 consumer_key consumer_secret;

put 'Authorization: OAuth oauth_consumer ';

do _n_=1 to dim(oauth);

   oauth[_n_]=quote(symget(vname(oauth[_n_])));

   if _n_>1 then put +(-1) ',';

   put +5 oauth[_n_]= @;

end;

run;

filename out temp;

proc http method='post' headerin=headIn url="http://localhost:8681/OAuth/token" out=out;

With careful counting of the number of quotation marks needed, you can do what you are looking to get without using any functions.

%let consumer_key=cheezwhiz;

%let consumer_secret=coorslight;

filename cURL pipe "curl -H ""Authorization: OAuth oauth_consumer consumer_key=""""&consumer_key"""", consumer_secret=""""&consumer_secret""""""";

filename cURL list;

NOTE: Fileref= CURL

      Physical Name= curl -H "Authorization: OAuth oauth_consumer consumer_key=""cheezwhiz"", consumer_secret=""coorslight"""

Side note, I've never seen an OAuth scheme like this, or that required quoting of the key and secret in the heading before, but I'll assume you know what you're doing on this front.

Frequent Contributor
Posts: 92

Re: Filename quote fun!

FriedEgg,

Thank you for your thoughts and code.  I prefer cURL over proc http because I can simultaneously submit up to 3 requests at once.  However, I may try proc http if I run into significant issues accessing the api via cURL.

Also, I try to carefully count the quotes to see if I can construct the header without any functions.

Thanks again,

Bill

Respected Advisor
Posts: 3,156

Re: Filename quote fun!

Posted in reply to BillJones

On a side note, I have to remind you that given the current direction SAS and other IT industries are heading, proc http may live longer than curl. My company for example, has just recently decided moving to server SAS (Grid) and eradicating PC SAS completely in a short period. Many legacy SAS code, especially those involving OS command and third party utilities (such as curl) have to be modified.

Haikuo

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 695 views
  • 6 likes
  • 4 in conversation