gemeni ai sas

Integrating Gemini AI into SAS:

This post will walk you through the integration of Google’s Gemini AI Model using its comprehensive API.

Gemini AI is a powerful LLM from Google AI noted for its conversational abilities and knowledge generation. LLMs excel in these areas:

  • Text Summarization: Condensing large blocks of text into key points.
  • Text Generation: Creating new text formats (e.g., creative writing, code snippets).
  • Translation: Converting languages.
  • Question Answering: Providing answers from information sources.

Steps to Integrate Gemini Into SAS

Step 1 : Get API Key

The Gemini API is currently available for free. There can be charges to use the API in future.

An API key is required to use the API. You can generate one by following this link. Make sure to store your API key securely for later use.

Step 2 : Enter API Key and Prompt

The user’s inputs for the API key and the search query are assigned to two macro variables: api_key and prompt. The API key should not have quotation marks around it, but the search query should. Do not delete the percentage signs (%) or the quotation marks in the “prompt” macro variable.

SAS Program

The following SAS program gets answers from the Google Gemini AI Model when users ask questions.Make sure to update your API key on line 1.

%let api_key= AIzaSyA08gG_1Li5_J3iGm6QQgCV62T3SkCp4tE;
%let prompt = %str(%"can you give insights for the dataset%");
%let url =;

/* Body of the POST request */
filename in temp;

data _null_;
	file in;
	put "{";
	put '"contents": [{"parts":[{"text": '"&prompt.}]}]";
	put "}";

/* reference that file as IN= parm in PROC HTTP POST */
filename resp temp;

proc http method="POST" url="&url." ct="application/json" in=in out=resp;

/* parse the JSON response */
libname response JSON fileref=resp;

/* Output the parsed JSON data */
proc print data=response.ALLDATA;

proc datasets library=response;

/* Format JSON in presentable format */
data outdata;
	set response.content_parts;

	do row=1 to max(1, countw(text, '0A'x));
		outvar=scan(text, row, '0A'x);
	drop text;

proc template;
	define style styles.test;
		style report / background=_undef_ /* Remove the background color */

		/* Remove the frame around the report */
		style header / 
			background=_undef_ /* Remove the background color of the header */

		/* Remove the border at the bottom of the header */
		style data / borderwidth=0;

		/* Remove all borders for data cells */

ods html file="/home/9to5sas/outputs/test.html" 

proc report data=outdata nowd style(report)=[background=_undef_ rules=cols 
	column outvar;
	define outvar / display "";

ods html close;


Here is the output produced by the SAS program above.

gemeni ai sas

here is another use case:

%let prompt = %str(%"can you analyise the dataset%");
gemeni ai sas

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.