We’re smarter together. Learn from this collection of community knowledge and add your expertise.

SAS Studio Custom Task Tuesday: How to Create the Dataset Directory Search Task

by SAS Super FREQ ‎01-17-2017 04:41 PM - edited ‎03-27-2017 09:56 AM (2,366 Views)

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. Custom Task Tuesday.jpg


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:



Step 1: Getting Started

 new task.png

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.



Step 2: Naming and Saving the 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 edity.pngbutton in the upper left corner of the task to bring up this option screen:




Step 3: Creating the Metadata Portion of the Task


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:



        <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"
                     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"
                     promptMessage="Enter keyword."
                     missingMessage="Missing text.">Keyword: </Option>
</Options> </Metadata>



Step 4: Creating the UI Portion of the Task


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:



   <Container option="DATATAB">
       <Group option="GROUPTEXT" open="true">
           <OptionItem option="labelTEXT1"/>
           <OptionItem option="LIBNAME"/>
           <OptionItem option="labelTEXT2"/>
           <OptionItem option="KEYWORD"/>



Step 5: Creating the Code Template Portion of the Task


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;
       set CONTENTS (keep = MEMNAME NAME);
       DATASET = upcase(MEMNAME);
       VARIABLE = upcase(NAME);
data CONTENTS_dataset;
       set CONTENTS;
       where dataset contains upcase("$KEYWORD");
       drop MEMNAME NAME;
data CONTENTS_variable;
       set CONTENTS;
       where variable contains upcase("$KEYWORD");
       drop MEMNAME NAME;
title "Observations where $KEYWORD was in the dataset name:";
proc print data = CONTENTS_dataset;
title "Observations where $KEYWORD was in the variable name:";
proc print data = CONTENTS_variable;


Step 6: Run the Dataset Directory Search Task


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 save.pngbutton to save, then click the run.pngbutton to open the task. Make your selections, then click run.pngagain to watch it run!


Want to try it yourself?

Get the code from the zip file at the end of this article or from GitHub.


Take Me to GitHub!


Your Turn

Can you think of a way to customize or improve the functionality of this task? Post it in the comments below!

by Valued Guide
on ‎01-19-2017 04:22 AM
this routine overwrites a work library table.
When you wish to avoid that and have no control over the environment, consider using _DATA_
See the SAS Global Forum paper 2120-2016 "More Hidden Base SAS® Features to Impress Your Colleagues"
by SAS Super FREQ
on ‎01-20-2017 10:29 AM

Thank you @Peter_C for your feedback! That would be a great addition to this task. I'll consider using that in the future but if you are interested in adding it to this task yourself, you can easily download my task, edit it, and save a new version. :)

Thanks again for your interest!

by SAS Super FREQ
on ‎01-20-2017 11:18 AM

I have attached a new version of the task that uses _DATA_ and will not create any datasets in the WORK library. Thanks again for the idea and the help @Peter_C! :)

by Occasional Contributor AllanBowe
on ‎09-11-2017 03:10 AM

Very nice explanation!   To anyone who wishes to expand the functionality to include data values, the following macro (part of the core library) may be of use: mp_searchdata

Your turn
Sign In!

Want to write an article? Sign in with your profile.

Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.