SAS Viya Copilot is here for Code Assistance! Throughout this post, I'll explore using SAS Viya Copilot for code generation and explanation, and compare it to the general-purpose LLM outputs I demonstrated at SAS Innovate. This post goes through step-by-step examples of prompting SAS Viya Copilot about data exploration, graph creation, and creating a functional macro program.
If you're already a SAS Viya user, your SAS administrator will need to activate Copilot in Environment Manager.
To do so, navigate to Manage Environment from the Applications menu> click Activate SAS Viya Copilot> select your geographic region> Copilot activates> SAS Viya Copilot is active!
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
Once active, you'll see the Copilot icon in the top right corner of your interface. Click it, and the chatbot panel appears on the right side of your screen.
Copilot supports a range of code assistance tasks — it can generate new code, provide examples, add comments, explain logic, reformat, refine, and identify problems in existing code. You can interact through the chatbot window, or work inline directly in your program by pressing Ctrl + I or highlighting a section of code. From there, you can type a specific request or use / to access a list of quick actions.
At SAS Innovate, I presented a demo comparing ChatGPT and Claude for SAS code generation. Using general-purpose LLMs was significantly more efficient than writing code from scratch — but working with them revealed some real limitations.
The most common issues I encountered were:
These aren't dealbreakers for general exploration, but they're significant barriers to production use. That's what makes SAS Viya Copilot so exciting.
Running the same prompts through SAS Viya Copilot, here's what stood out:
For the rest of this post, I'll walk through sample prompts and the responses SAS Viya Copilot generated. I’ll start small with data exploration, and end with prompting Copilot to generate a functioning macro.
If you're new to SAS Viya Copilot, this should give you a practical starting point for understanding what it can do.
Prompt 1:
Please treat SAS as the default programming language for this conversation.
When I ask for code:
Prompt 2:
Provide a description of the CARS table in the SASHELP library.
What are the columns and basic attributes?
Prompt 3:
Generate a PROC PRINT step that will print the first 20 rows of SASHELP.CARS.
Prompt 4:
Write a DATA step that computes the following new columns:
If it is found, then assign the number 2 to Doors.
Also search Model for the string 4dr.
If it is found, then assign the number 4 to doors.
Otherwise assign a missing value.
Keep the following columns: Make, Model, Type, Origin, MPG_Avg, Doors
/* Create a new table WORK.CARS_MPG with the requested columns and logic */
data cars_mpg;
set sashelp.cars;
/* Compute average MPG */
MPG_Avg = mean(MPG_City, MPG_Highway);
/* Assign number of doors based on Model */
if index(model, '2dr') then Doors = 2;
else if index(model, '4dr') then Doors = 4;
else Doors = .;
/* Keep only required columns */
keep Make Model Type Origin MPG_Avg Doors;
run;
Prompt 5:
Could you generate a similar result using PROC SQL?
proc sql;
/* Create a new table WORK.CARS_MPG_SQL with the requested columns and logic */
create table cars_mpg_sql as
select
Make,
Model,
Type,
Origin,
/* Compute average MPG */
mean(MPG_City, MPG_Highway) as MPG_Avg,
/* Assign number of doors based on Model string contents */
case
when index(Model, '2dr') > 0 then 2 /* 2 doors if '2dr' is found */
when index(Model, '4dr') > 0 then 4 /* 4 doors if '4dr' is found */
else . /* Otherwise missing */
end as Doors
from sashelp.cars;
quit;
Prompt 6:
Generate a report that counts the number of cars for each value of Type.
proc freq data=sashelp.cars;
tables Type;
title "Count of Cars by Type";
run;
Prompt 7:
How can I order the report by descending frequency?
proc freq data=sashelp.cars order=freq;
tables Type;
title "Count of Cars by Type (Descending Frequency)";
run;
Prompt 8:
Enhance the PROC FREQ step to create a macro variable that allows a user to filter the report for a specific make of car.
%let CarMake=Toyota; /* User can change this value to filter for a specific car make */
proc freq data=sashelp.cars(where=(Make="&CarMake")) order=freq;
tables Type;
title "Count of Cars by Type for Make=&CarMake (Descending Frequency)";
run;
Prompt 9:
Use inline commands to change the macro variable name CarMake to CrMk.
Select Apply All to accept the changes to the code.
%let CrMk=Toyota;
/* User can change this value to filter for a specific car make */
proc freq data=sashelp.cars(where=(Make="&CrMk")) order=freq;
tables Type;
title "Count of Cars by Type for Make=&CrMk (Descending Frequency)";
run;
Prompt 10:
Create a vertical bar chart that displays the number of cars for each value of Type.
proc sgplot data=sashelp.cars;
vbar Type;
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
For the rest of the prompts, I highlighted the relevant lines of code and prompted Copilot.
Prompt 11:
Order the bars by descending frequency.
proc sgplot data=sashelp.cars;
vbar Type / categoryorder=respdesc;
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
Prompt 12:
Add data labels to each bar.
proc sgplot data=sashelp.cars;
vbar Type / categoryorder=respdesc datalabel; /* Add data labels to each bar */
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
Prompt 13:
Modify this step to make each bar a different color.
proc sgplot data=sashelp.cars;
vbar Type / categoryorder=respdesc datalabel group=Type; /* Each bar colored by Type, with data labels */
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
proc sgplot data=sashelp.cars;
styleattrs datacolors=(blue green red orange purple gray); /* Customize colors if desired */
vbar Type / categoryorder=respdesc datalabel group=Type;
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
Prompt 14:
Modify this step so that the bars and key do not have outlines.
NOTE: There were some troubles due to versioning.
The first response produced an error:
I pasted the error and prompted as follows:
I got this error:
79 styleattrs datacolors=(blue green red orange purple gray)
80 outlinecolors=(transparent); /* Remove outlines from legend/key */
------------- 22 76 ERROR 22-322: Syntax error, expecting one of the following:
;, AXISBREAK, AXISEXTENT, BACKCOLOR, DATACOLORS, DATACONTRASTCOLORS, DATAFILLPATTERNS,
DATALINEPATTERNS, DATASYMBOLS, WALLCOLOR.
ERROR 76-322: Syntax error, statement will be ignored.
I then added the NOOUTLINE option to the VBAR statement as directed and achieved the desired results.
proc sgplot data=sashelp.cars;
styleattrs datacolors=(blue green red orange purple gray); /* Remove outlines from legend/key */
vbar Type / categoryorder=respdesc datalabel group=Type nooutline; /* Remove bar outlines */
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
Prompt 15:
Modify the step to make the bar colors shades of dark to light pink using the RGB color naming scheme.
proc sgplot data=sashelp.cars;
styleattrs datacolors=(CXc71585 CXdb7093 CXff69b4 CXffb6c1 CXffe4e1 CXfff0f5); /* Dark to light pink shades */
vbar Type / categoryorder=respdesc datalabel group=Type nooutline;
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
Prompt 16:
Modify the code as follows:
Define a macro and use macro conditional processing.
If the value of the macro variable is usa, then UPCASE the value in the title.
Else, PROPCASE the value.
If the value is not Asia, USA, or Europe, write a note to the log that the valid values are Asia, Europe, or USA in any case.
If no value is provided, create the bar chart for all origins and write a note to the log.
%macro plot_cars(origin=);
%let _origin = %upcase(&origin);
%if %length(&_origin) = 0 %then %do;
/* No value provided: plot all origins */
proc sgplot data=sashelp.cars;
styleattrs datacolors=(CXc71585 CXdb7093 CXff69b4 CXffb6c1 CXffe4e1 CXfff0f5); /* Dark to light pink shades */
vbar Type / categoryorder=respdesc datalabel group=Type nooutline;
yaxis label="Number of Cars";
title "Number of Cars by Vehicle Type";
run;
%put NOTE: No origin value provided. Chart includes all origins.;
%end;
%else %if &_origin = USA %then %do;
/* USA: title in uppercase */
proc sgplot data=sashelp.cars(where=(upcase(Origin)="USA"));
styleattrs datacolors=(CXc71585 CXdb7093 CXff69b4 CXffb6c1 CXffe4e1 CXfff0f5); /* Dark to light pink shades */
vbar Type / categoryorder=respdesc datalabel group=Type nooutline;
yaxis label="Number of Cars";
title "%upcase(&origin) Cars by Vehicle Type";
run;
%end;
%else %if &_origin = ASIA or &_origin = EUROPE %then %do;
/* Asia or Europe: title in proper case */
proc sgplot data=sashelp.cars(where=(upcase(Origin)="&_origin"));
styleattrs datacolors=(CXc71585 CXdb7093 CXff69b4 CXffb6c1 CXffe4e1 CXfff0f5); /* Dark to light pink shades */
vbar Type / categoryorder=respdesc datalabel group=Type nooutline;
yaxis label="Number of Cars";
title "%sysfunc(propcase(&origin)) Cars by Vehicle Type";
run;
%end;
%else %do;
/* Invalid value: note to log */
%put NOTE: Invalid origin value. Valid values are Asia, Europe, or USA (any case).;
%end;
%mend;
Example Usage:
%plot_cars(origin=usa)
%plot_cars(origin=Asia)
%plot_cars(origin=Europe)
%plot_cars(origin=Australia)
%plot_cars()
SAS Viya Copilot is a meaningful step forward for SAS programmers — not because it replaces your expertise, but because it accelerates it. Across data exploration, PROC SGPLOT, and macro generation, Copilot consistently produced accurate, documentation-grounded code with less back-and-forth than general-purpose LLMs. The built-in data security removes the biggest practical barrier to using AI assistance in real workflows, and small touches like inline editing and insert-at-cursor make it feel native to the SAS environment. Give it a try and see how it enhances your workflow!
For more on SAS Viya Copilot for Code Assistance, check out the following resources:
Find more articles from SAS Global Enablement and Learning here.
Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.