BookmarkSubscribeRSS Feed

Getting Started with SAS Viya Copilot for Code Assistance

Started ‎07-01-2026 by
Modified ‎07-01-2026 by
Views 290

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.

 

 

Activating SAS Viya Copilot

 

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!

 01_CJC_Picture39.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

02_CJC_Picture40-300x192.png

 

03_CJC_Picture41-300x142.png

 

04_CJC_Picture42.png

 

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.

 

05_CJC_Picture1-1.png

 

06_CJC_Picture36.png

 

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.

 

07_CJC_Picture3-1.png

 

 

Why SAS Viya Copilot Matters

 

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:

 

  • Hallucinations increased with complexity. When working with PROC SGPLOT, the models invented non-existent options, misassigned options to the wrong statements, or omitted required statements entirely. Macro generation often required several follow-up queries before producing functioning output.
  • Data security is a genuine concern. For the Innovate demo, I used SASHELP.CARS, which is public data — so I could query it directly without risk. In practice, most SAS users are working with sensitive or proprietary data. General-purpose LLMs use your prompts to train their models by default, which creates a real risk of data exposure. The workaround is generating sample syntax and manually adapting it; however, this adds friction, time, and room for error.

 

These aren't dealbreakers for general exploration, but they're significant barriers to production use. That's what makes SAS Viya Copilot so exciting.

 

 

How SAS Viya Copilot Addresses These Gaps

 

Running the same prompts through SAS Viya Copilot, here's what stood out:

 

  • Lower hallucinations on PROC SGPLOT and macros. The generated code was more accurate overall, with options correctly placed on the right statements. When removing bar chart outlines, there were troubles; however, when the error was produced, Copilot generated the correct option for the current Viya version on the second prompt. When working with macros, responses produced functioning output without follow-up correction.
  • Data security is built in. Customer data is never used to train the underlying LLM. SAS Viya securely transmits requests to the SAS Viya Copilot service running in a Microsoft Azure environment dedicated to SAS. SAS and Microsoft do not retain your data or content — it is stored only transiently during request processing. This is a meaningful shift: you can now generate code directly against your actual SAS data without the security tradeoffs that come with general-purpose tools.
  • Insert at cursor. Generated code can be inserted directly into your program at the cursor position — no tab-switching, no copy-pasting. It's a small feature that adds up to real time saved in a real workflow.
  • RAG-powered and documentation-grounded. SAS Viya Copilot is trained using Retrieval Augmented Generation (RAG), meaning it pulls from official SAS documentation — documentation that procedure developers actively maintain. Each chat response includes citations and detailed explanations, so you're not left guessing where the syntax came from.

 

 

Prompt and Response Examples

 

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.

 

 

Exploring Data

 

Prompt 1:

Please treat SAS as the default programming language for this conversation.

When I ask for code:

 

  • Use Base SAS (DATA step, PROC SQL, PROCs)
  • Include brief explanations of what the code is doing
  • Minimize steps and use the most efficient methods when possible
  • Follow best practices for readability and teaching

 

08_CJC_Picture4-1.png

 

Prompt 2:

Provide a description of the CARS table in the SASHELP library.

 

What are the columns and basic attributes?

 

09_CJC_Picture5-1.png

 

10_CJCPicture6-1.png

 

Prompt 3:

Generate a PROC PRINT step that will print the first 20 rows of SASHELP.CARS.

 

11_CJC_Picture7-1.png

 

 

Manipulating Data with the DATA Step and PROC SQL

 

Prompt 4:

Write a DATA step that computes the following new columns:

 

  • MPG_Avg: average of MPG_City and MPG_Highway
  • Doors: Search the Model column for the string '2dr'.

 

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

 

12_CJC_Picture8.png

 

/* 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?

 

13_CJC_Picture9.png

 

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;

 

 

Creating and Enhancing a PROC FREQ Report

 

Prompt 6:

Generate a report that counts the number of cars for each value of Type.

 

14_CJC_Picture10.png

 

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?

 

15_CJC_Picture11.png

 

proc freq data=sashelp.cars order=freq;
    tables Type;
    title "Count of Cars by Type (Descending Frequency)";
run;

 

 

Creating a Macro Variable

 

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.

 

16_CJC_Picture12.png

 

%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.

 

17_CJC_Picture13.png

 

18_CJC_Picture14.png

 

19_CJC_Picture15.png

 

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;

 

 

Creating and Enhancing a Bar Chart with PROC SGPLOT

 

Prompt 10:

Create a vertical bar chart that displays the number of cars for each value of Type.

 

20_CJC_Picture16.png

 

proc sgplot data=sashelp.cars;
    vbar Type;
    yaxis label="Number of Cars";
    title "Number of Cars by Vehicle Type";
run;

 

21_CJC_Picture37.png

 

For the rest of the prompts, I highlighted the relevant lines of code and prompted Copilot.

 

22_CJC_Picture17.png

 

Prompt 11:

Order the bars by descending frequency.

 

23_CJC_Picture18.png

 

proc sgplot data=sashelp.cars;
    vbar Type / categoryorder=respdesc;
    yaxis label="Number of Cars";
    title "Number of Cars by Vehicle Type";
run;

 

24_CJC_Picture19.png

 

Prompt 12:

Add data labels to each bar.

 

25_CJC_Picture20.png

 

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;

 

26_CJC_Picture21.png

 

Prompt 13:

Modify this step to make each bar a different color.

 

27_CJC_Picture22.png

 

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;

 

28_CJC_Picture23.png

 

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;

 

29_CJC_Picture24.png

 

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:

 

30_CJC_Picture25.png

 

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.

 

31_CJC_Picture26.png

 

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;

 

32_CJC_Picture27.png

 

Prompt 15:

Modify the step to make the bar colors shades of dark to light pink using the RGB color naming scheme.

 

33_CJC_Picture28.png

 

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;

 

34_CJC_Picture29.png

 

 

Creating a Macro Program with Error Handling

 

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.

 

35_CJC_Picture30.png

 

%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)

 

36_CJC_Picture31.png

 

%plot_cars(origin=Asia)

 

37_CJC_Picture32.png

 

%plot_cars(origin=Europe)

 

38_CJC_Picture33.png

 

%plot_cars(origin=Australia)

38a_Picture34.png

 

 

 

%plot_cars()

39_CJC_Picture35.png

 

 

Conclusion

 

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!

 

 

Additional Resources:

 

For more on SAS Viya Copilot for Code Assistance, check out the following resources:

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
‎07-01-2026 12:56 PM
Updated by:

Viya Copilot Motion Graphic.gifViya Copilot Motion Graphic

Ready to see what SAS Viya Copilot can do?

Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.

Get Started →

SAS AI and Machine Learning Courses

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.

Get started

Article Tags