Run ChatGPT in SAS

Deepanshu Bhalla Add Comment , ,

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.

Integration of ChatGPT in SAS
What does ChatGPT bring within SAS

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.

Steps to integrate ChatGPT within SAS
Step 1 : Get API Key

Visit this link - 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.


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.

Step 2 : Enter API Key and Question

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.

SAS Program
%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  '"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": '"&question }]";
put "}";

/* reference that file as IN= parm in PROC HTTP POST */
filename resp "%sysfunc(getoption(WORK))/echo.json";
proc http 
 headers "Authorization" = "Bearer &api_key.";
/* 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));
  drop content;
proc report data=outdata ;
column outvar;
define outvar / display "" style(column)=[cellwidth=6in fontsize=10pt asis=ON];

Integrate ChatGPT within SAS
How to access GPT-4

To access GPT-4, you can replace gpt-3.5-turbo with gpt-4 in the above SAS code. OpenAI keeps on updating their models and they have several versions of them but they make sure that users get the latest model version in gpt-4 or gpt-3.5-turbo.

Let's ask ChatGPT to debug SAS code

ChatGPT-3.5 is not so accurate especially in debugging programs of enterprise softwares as compared to open source programming languages. With the release GPT-4, it has significantly improved both in terms of generating and debugging SAS code. 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".
How to check JSON before calling ChatGPT

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;
put _infile_;

See JSON in the LOG Window.

 "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "3+3" }]
How to check the status of HTTP Request
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.

Print Raw Response to Log Window
By using jsonpp( ) function you can print raw JSON returned as response to log window.
data _null_;
 rc = jsonpp('resp','log');
   "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": "\n\n6"
       "finish_reason": "stop",
       "index": 0
Related Posts
Spread the Word!
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "Run ChatGPT in SAS"
Next → ← Prev