smackersz88
smackersz88

Reputation: 93

PROC HTTP SAS Macro issue

I'm using a macro that allows you to prompt CHAT-GPT 3.5 (or trying to at least) within a SAS environment. Here is the code.

   options mprint mlogic symbolgen;


Obviously not going to show you my API-Key (Fake example provided) but the code works fine outside of being wrapped in a macro but when I run it inside a macro I get this error.

SYMBOLGEN:  Macro variable SYS_PROCHTTP_STATUS_CODE resolves to 404
 MLOGIC(CHATGPT):  %IF condition (&SYS_PROCHTTP_STATUS_CODE. NE 200) is TRUE
 MLOGIC(CHATGPT):  %PUT An error occurred. HTTP &SYS_PROCHTTP_STATUS_CODE.: &SYS_PROCHTTP_STATUS_PHRASE
 SYMBOLGEN:  Macro variable SYS_PROCHTTP_STATUS_CODE resolves to 404
 SYMBOLGEN:  Macro variable SYS_PROCHTTP_STATUS_PHRASE resolves to Not Found
 An error occurred. HTTP 404: Not Found
 MLOGIC(CHATGPT):  %ABORT 
 ERROR: Execution terminated by an %ABORT statement.

Is it something to do with the headers authorisation statement in PROC HTTP?. Here is the code outside of the macro.

    %let api_key= ; 
    %let question = %str(%"sas code to transpose data%");
    %let question = %str(%"debug 'proc print data=mydf; vars myvar; run;' %");
    
    /* Body of the POST request */
    filename in temp;
    data _null_;
    file in;
    put;
    put "{";
    put  '"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": '"&question }]";
    put "}";
    run;
    
    

Upvotes: 0

Views: 575

Answers (1)

Tom
Tom

Reputation: 51601

If you want to use a data step to write the value of a macro variable to a text file it is probably best to first get the value from the macro variable into an actual variable. (Note: Dataset variables can only hold 32K bytes and macro variables can hold 64K bytes.) This will mean that the macro does not need to worry about macro quoting the value to use it. So if the text includes & or % characters the macro processor will not try to resolve the references.

data _null_;
  file in;
  length text $32767;
  text=symget('text');
  put '{"model": "gpt-3.5", "messages": [{"role": "user", "content": ' 
      text :$quote. '}]}'
  ;
run;

If you want to generate the AUTHORIZATION headername value in the PROC HTTP call then perhaps it is better to parameterize the macro to expect the full value, instead of just part of the value. So include the text Bearer (if that is required) in the macro call.

%macro chatgpt(authorization= ......);
   ...

headers "Authorization" = &authorization. ;
   ...

And then when calling the macro pass the authorization value, including the quotes. Again if the value might include macro triggers like & or % that you don't want the macro processor to modify you can enclose the string in single quotes in the macro call.

%chatgpt(authorization='Bearer xxxxxx',......);  

Upvotes: 1

Related Questions