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 sashelp.cars dataset%");
%let url = https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=&api_key.;

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

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

/* 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;
run;

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

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

proc datasets library=response;
quit;

/* 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);
		output;
	end;
	drop text;
run;

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

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

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

		/* Remove all borders for data cells */
	end;
run;

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

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

ods html close;

Output

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 sashelp.cars dataset%");
gemeni ai sas

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

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

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.