Forms and prompts make SAS Viya jobs flexible and interactive. They let users select filters, parameters, and options before a job runs. Whether you’re building a report that returns sales for a specific day, or a process that summarizes data between two dates, date controls let users supply that information directly in the job interface. In SAS Viya, you can add date controls to both HTML forms and JSON-based step prompts. In this article, I’ll discuss how to create date selection controls, and how to handle the parameter values they generate in a job definition.
To build a date control in an HTML form, use the <input> element with the type attribute set to date.
Here’s a simple example:
<label for="startDate">Select a start date:</label>
<input type="date" id="startDate" name="startDate">
Which creates the following control:
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
The name attribute must match the macro variable you plan to reference in your SAS code — in this case, selectDate.
You can also:
These options make it easy to tailor your input to the needs of your job.
Date controls can also be used in JSON-based prompt forms. You can use the Date and Time Picker control to allow the user to select date or time values.
This control supports:
Forms built using the Date and Time Picker control look like this:
Like with the HTML control, the ID value used by the picker must match the macro variable used in the program code.
Whether you use a JSON form or an HTML form, you need to consider how the parameter values are passed to the code. Once a user selects a date, the macro variable received by the SAS job looks like this:
2020-01-01
The macro variable containing the date selection is not a SAS date value – in other words, it is not numeric. Since SAS stores dates as the number of days since January 1, 1960, you need to convert the value before using it in expressions or comparisons.
There are a few ways to handle this. You can use the INPUT function with an appropriate informat wherever you reference the date:
where Date = input("&selectDate", yymmdd10.);
While this technique works, it means you must repeat the conversion each time you use the variable.
Another approach is to convert the date once at the top of the program and store it in a new macro variable:
%let SASDate = %sysfunc(inputn("&selectDate", yymmdd10.));
Now you can use &SASDate anywhere in your code, and it behaves as a numeric SAS date:
where Date >= &SASDate;
This makes your code easier to read and maintain.
Working with date values in SAS Viya jobs isn’t complicated, but it’s important to be aware of how SAS represents time. By setting up your input form carefully and converting user-provided dates to numeric SAS dates, you’ll make your jobs more reliable, maintainable, and user-friendly.
Find more articles from SAS Global Enablement and Learning here.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.