The first task that we are going to focus on during Custom Task Tuesday is a task on macro proc execution, which is probably the most editable and customizable of all of the tasks we will discuss.
The Macro Proc Execution allows you to select a dataset and select any number of procs from a list to run on the chosen dataset.
Here is how this task's finished product will look:
In my implementation, each of these procs is enclosed in a macro that is executed conditionally on whether or not the checkbox is checked. My macros contain only one proc for the purposes of this blog post, but this concept becomes even more powerful when you think about the idea of executing your own macros that you may already have on any dataset with the click of a checkbox.
In SAS Studio, under the Task and Utilities section, open a “New Task” as well as the “Sample Task.” We will copy and paste the necessary Velocity Template code from the Sample Task to our task.
Name: Macro Proc Execution
Description: This task allows you to specify a dataset as well as any number of procs from a list to run on the dataset. The list of procs include: proc print, proc freq, proc means, and proc corr. These procs will run with their default setting and therefore should be appropriate for any dataset that the user may choose.
At the top of the VTL code for your New Task, you will need to fill in the Name and Description portions as shown below:
After you’ve done that, you should save this task to your My Tasks folder, so you don’t lose it. Click the button in the upper left corner of the task to bring up this option screen:
To make things easier on ourselves, we are going to steal VTL code from the Sample Task. From our “finished product,” you can see that our task is going to include one dataset selector as well as a group of checkboxes, which will have 4 checkboxes. Find the several lines of code in the Sample Task that correspond with a dataset selector and checkboxes in the Metadata section, and copy and paste them into the same place in your newly created Macro Proc Execution task. Edit the code you copied (change the labels) to correspond with what we want as our finished product.
This is what your finished Metadata portion should look like:
<Metadata> <DataSources> <DataSource name="DATASOURCE"> </DataSource> </DataSources> <Options> <Option inputType="string" name="DATATAB">DATA</Option> <Option inputType="string" name="DATAGROUP">DATA</Option> <Option inputType="string" name="GROUPCHECK">ANALYSIS</Option> <Option inputType="string" name="labelCHECK">Select the types of output you would like for your dataset. Choose at least one.</Option> <Option defaultValue="0" inputType="checkbox" name="chkFREQ">Frequency Analysis</Option> <Option defaultValue="0" inputType="checkbox" name="chkMEANS">Statistical Summary</Option> <Option defaultValue="0" inputType="checkbox" name="chkPRINT">Data Print Out</Option> <Option defaultValue="0" inputType="checkbox" name="chkCORR">Correlation Matrix</Option> </Options> </Metadata>
Each object that we just put code for in the metadata portion will have corresponding code in the UI section. The code for the UI portion is what determines the order items are displayed in the finished task.
Just like we did in the previous step, find the lines of code that correspond with the dataset selector and checkbox group in the UI section of the Sample Task, and copy and paste them into the same place in your task. Edit the code you copied (make sure your names in the Metadata section match the option= lines in the UI section) to correspond with what we want as our finished product.
This is what your finished UI portion should look like:
<UI> <Container option="DATATAB"> <Group open="true" option="DATAGROUP"> <DataItem data="DATASOURCE"/> </Group> <Group open="true" option="GROUPCHECK"> <OptionItem option="labelCHECK"/> <OptionItem option="chkPRINT"/> <OptionItem option="chkFREQ"/> <OptionItem option="chkMEANS"/> <OptionItem option="chkCORR"/> </Group> </Container> </UI>
This is the portion of the task that contains your SAS Code. Velocity Template Language has its own macro variables, and each of our UI elements has one. This is how you make sure that your code works with what the user selects in the interface you created. When you select an option in the UI, the value of the VTL macro variable will change immediately. As you'll see below, VTL macro variables are denoted by a dollar sign, such as $DATASOURCE.
Since the code for this task is quite short, the full SAS code is below:
%macro analysis; *Value for print checkbox: 1 if checked, 0 if unchecked.; %if $chkPRINT = 1 %then %do; proc print data = $DATASOURCE; run; %end; *Value for freq checkbox: 1 if checked, 0 if unchecked.; %if $chkFREQ = 1 %then %do; proc freq data = $DATASOURCE; run; %end; *Value for means checkbox: 1 if checked, 0 if unchecked.; %if $chkMEANS = 1 %then %do; proc means data = $DATASOURCE; run; %end; *Value for corr checkbox: 1 if checked, 0 if unchecked.; %if $chkCORR = 1 %then %do; proc corr data = $DATASOURCE; run; %end; %mend analysis; %analysis;
You’re finished! You just created a task that will allow a user to select any dataset and run a proc on it from your custom list of procs. Click the button to save, then click the button to open the task. Make your selections, then click again to watch it run!
Get the code from the zip file at the end of this article or from GitHub.
Can you think of a way to customize or improve this task? Post it in the comments below!
SAS is headed back to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team.
Interested in speaking? Content from our attendees is one of the reasons that makes SAS Innovate such a special event!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.