Have you ever wanted to search a SAS library for a keyword, to find a specific dataset or list of datasets that include a phrase, prefix, or suffix in their name or variable names? If so, the SAS Studio custom task functionality is here to help. The task we are going to construct with this week’s post is a dataset directory search task.
The dataset directory search task allows the user to input the name of their SAS library and the key word they want to search for, and then outputs a list of datasets that contain the keyword in their name, and a list of datasets that contain the keyword in their variable names.
Here’s what the finished product looks like:
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: Dataset Directory Search
Description: Search a SAS library for user-specified key words.
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:
Just like in previous blog posts, we are going to use the built-in Sample Task to borrow VTL code. From our “finished product,” you can see that all we are going to need are two text boxes. Find the code that correspond with the text boxes in the Metadata section of the Sample Task, and copy and paste them into the same place in your task. Edit to code you copied to correspond with what we want as our finished product.
This is what your finished Metadata portion should look like:
<Metadata> <Options> <Option name="DATATAB" inputType="string">DATA </Option> <Option name="GROUPTEXT" inputType="string">TEXT SEARCH </Option> <Option name="labelTEXT1" inputType="string">Enter the libname of the library you would like to search. </Option> <Option name="LIBNAME" defaultValue="WORK" inputType="inputtext" indent="1" required="true" promptMessage="Enter libname." missingMessage="Missing text.">Libname: </Option> <Option name="labelTEXT2" inputType="string">Enter the keyword you would like to search for. </Option> <Option name="KEYWORD" defaultValue="KEY" inputType="inputtext" indent="1" required="true" promptMessage="Enter keyword." missingMessage="Missing text.">Keyword: </Option>
In Custom Tasks, each object that we just put code for in the metadata portion requires corresponding code in the UI section. Just like we did in step 3, find the code that correspond with text boxes in the UI section of the Sample Task, and copy and paste them into the same place in your task. Edit to code you copied 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 option="GROUPTEXT" open="true"> <OptionItem option="labelTEXT1"/> <OptionItem option="LIBNAME"/> <OptionItem option="labelTEXT2"/> <OptionItem option="KEYWORD"/> </Group> </Container> </UI>
This is the portion of the task that contains your SAS Code, and the part that actually makes the options of the user work with your SAS code. Velocity Template Language has its own macro variables, and each of our UI elements has one. When you select an option in the UI, the value of the VTL macro variable will change immediately.
This SAS code uses proc contents to identify the presence of key words in the user’s library (in dataset names and variable names). Two separate datasets are created for the presence of the key word in the name, and the presence of the key word in the variable name. If the datasets are non-empty, they are printed out with a proc print.
proc contents data = $LIBNAME._all_ noprint out=CONTENTS; run; data CONTENTS; set CONTENTS (keep = MEMNAME NAME); DATASET = upcase(MEMNAME); VARIABLE = upcase(NAME); run; data CONTENTS_dataset; set CONTENTS; where dataset contains upcase("$KEYWORD"); drop MEMNAME NAME; run; data CONTENTS_variable; set CONTENTS; where variable contains upcase("$KEYWORD"); drop MEMNAME NAME; run; title "Observations where $KEYWORD was in the dataset name:"; proc print data = CONTENTS_dataset; run; title "Observations where $KEYWORD was in the variable name:"; proc print data = CONTENTS_variable; run;
You’re finished! You just created a custom user interface to search a library for a keyword in dataset names and variables names. 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 the functionality of this task? Post it in the comments below!
Registration is open! SAS is returning 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. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.