Ex.1 Stata in Positron and Copilot
The goal of this exercise is to get familiar with Positron’s AI assistance features and how they can be used to write and execute Stata code. We go through a typical workflow of creating a Stata do file, writing code with AI assistance, and executing it in the console.
This exercise overviews the following features of Positron’s AI assistance:
- inline suggestions: accepting, rejecting, and customizing
- chat mode: asking for code generation, revision, and troubleshooting
- inline chat: revising specific code snippets without leaving the editor
- agent mode: automating code execution and debugging
- context monitoring
Watch the full video (18 min.) here:
Where to start?
Setup working folders properly:
- Copy folder with
ex01-explore-positronto your working directory - Open
ex01-explore-positronin Positron - Follow the instructions below
All examples are available:
- on GitHub: https://github.com/worldbank/ai4coding-examples
- on One Drive (WB only): ai4coding-examples
For this example, data is already contained in the folder, but you can also download it from ai4coding-data and place it your course folder.
Prompt examples and example details
Step 1. Create a script file in your editor
Let us call the file: second-positron-do-with-ai.do.
Step 2. Inline suggestions
Write comments at the top of the file and save it. Then proceed adding the details and use in-line suggestions.
Inline suggestions pop out automatically if you have enabled GitHub Copilot and Positron Assistant.
if it does not work, check: Positron Assistant Completions
Remember that there might be many suggestions and you can customize them:
Agent: code generation
Use the Chat window to ask for code generation for the steps outlined in the comments. For example: add following details to the file and ask AI to implement them:
/*
I want to:
1. Load exemplary data from data/raw/
2. Summarize descriptive statistics of all variables
3. Run a regression of income on individual characteristics
4. Create a scatter plot of income vs age
5. Create a box plot of income vs education levels
6. Save regression results and figures in an Excel file
*/Open Assistant chat and type:
Prompt
Add context by clicking on
+ Add contextand selecting the do file you are working on or adding console context with .Choose the model
and adjust the type of agent
if needed.Press
Send
Inspect the code and choose which parts to accept or reject
Use run button to execute the code
Interrupt request if it goes wrong.
Switch between different models (e.g. GPT-4, Claude)
Choose the right mode: chat, agent, edit or plan.
Monitor the context window size
- When it is too big, AI forgets or optimizes it, potentially losing some important details.
- Use
Show Chat Debug Viewto monitor the context and how it changes with each interaction.
In-line chat: code refactoring
Select the code that does not behave as expected. Press Ctrl + I, then ask the AI assistant to revise it.
- Select model and adjust settings if needed.
- If happy, accept the changes. If not, provide feedback and ask for another revision.
- Can be used with free models.
Agent: to revise and troubleshoot entire script
Iterate by adding requirements in the chat or in comments and ask AI to implement those step by step. For example:
Prompt
Prompt
Important: Be explicit about how Stata code should be executed.
AI may sometimes choose the wrong approach (for example, creating a Python script to call Stata) instead of using Positron’s built-in #executeCode command. When this happens, AI is preoccupied about how to run Stata rather than what code to write, which leads to suboptimal code generation.
To avoid this, always include the instruction about using #executeCode in your prompt when asking for code generation or revision in agent mode.

