In this post, you will learn how to use ChatGPT within SAS. As compared to the Python and R community, there hasn’t been much discussion within the SAS community about integrating ChatGPT into their software. Python even have official package for the same. Don’t worry – Now we also have solution in SAS as well.
You can ask about anything like you do in the official ChatGPT website. Within SAS environment, you may be interested in topics related to SAS functions, procedures, data steps or any other topic. You may even ask ChatGPT if your SAS program is not working, and you are having difficulty to debug the program. ChatGPT can also help you in understanding statistical concepts, or may help you in building or validating a predictive model.
Visit this link – platform.openai.com. Create your account via your Google or Microsoft email address. Most important step after creating your account is to get secret API key to access API. Copy and save your API key for future reference.
sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OpenAI will give you 18 dollars worth free trial to test APIs which will be expired by April 1,2023. Once free trial is over, you will have to pay $0.002 / 1000 tokens. You can call tokens as words. As per official documentation of ChatGPT, 1 token is approximately 4 characters or 0.75 words for English text.
These two macro variables api_key and question take user’s inputs as Secret API key and the question user wants to ask (i.e. search query). See the text highlighted in bold below. Make sure secret API key NOT to be in quotes. Also don’t remove percentage (%) and quotes in question macro variable.
%let api_key= sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
%let question = %str(%”sas code to transpose data%”);
/* 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;
/* reference that file as IN= parm in PROC HTTP POST */
filename resp “%sysfunc(getoption(WORK))/echo.json”;
proc http
method=”POST”
url=”https://api.openai.com/v1/chat/completions”
ct=”application/json”
in=in
out=resp;
headers “Authorization” = “Bearer &api_key.”;
run;
/* Tell SAS to parse the JSON response */
libname response JSON fileref=resp;
/* Format JSON in presentable format */
data outdata ;
set response.choices_message;
do row=1 to max(1,countw(content,’0A’x));
outvar=scan(content,row,’0A’x);
output;
end;
drop content;
run;
proc report data=outdata ;
column outvar;
define outvar / display “” style(column)=[cellwidth=6in fontsize=10pt asis=ON];
run;
ChatGPT is not so accurate especially in debugging programs of enterprise softwares as compared to open source programming languages. I guess it’s because of the less amount of FREE publicly available content on Internet for enterprise softwares. Let’s test if ChatGPT can debug this simple program proc print data=mydf; vars myvar; run; When you have quotes in your search query, enter it with single quotes instead of double.
%let question = %str(%”debug ‘proc print data=mydf; vars myvar; run;’ %”);
This is what ChatGPT returns. It was able to catch this simple error.
There is a syntax error in the above statement. It should be:
“`
proc print data=mydf; var myvar; run;
“`
The correct keyword to specify the variable(s) to be printed is “var” not “vars”.
The program below returns body portion of HTTP POST request you want to send. Here my search query is 3+3
data _null_;
infile in;
input;
put _infile_;
run;
See JSON in the LOG Window.
{
“model”: “gpt-3.5-turbo”, “messages”: [{“role”: “user”, “content”: “3+3” }]
}
PROC HTTP returns the status of request in LOG Window. Status code 200 means it has been done successfully. 400 refers to Bad Request (some issue in your JSON).
NOTE: 200 OK
NOTE: 400 Bad Request
SAS has also introduced macro variables for checking the status of HTTP Request. You need to have SAS 9.4 for accessing them.
%put HTTP Status code = &SYS_PROCHTTP_STATUS_CODE. : &SYS_PROCHTTP_STATUS_PHRASE.;
By using jsonpp( ) function you can print raw JSON returned as response to log window.
data _null_;
rc = jsonpp(‘resp’,’log’);
run;
{
“id”: “chatcmpl-6xxxxxxxxxxxxxxxxx”,
“object”: “chat.completion”,
“created”: 1678323037,
“model”: “gpt-3.5-turbo-0301”,
“usage”: {
“prompt_tokens”: 10,
“completion_tokens”: 3,
“total_tokens”: 13
},
“choices”: [
{
“message”: {
“role”: “assistant”,
“content”: “nn6”
},
“finish_reason”: “stop”,
“index”: 0
}
]
}
Read MoreListenData