Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » ADVANCED SAS » SAS Macros » Maximizing the Power of SAS Macro Variables: Using Them as Dates

Maximizing the Power of SAS Macro Variables: Using Them as Dates

BySubhro Posted onSeptember 18, 2022January 6, 2023 Last Updated:January 6, 2023
0 Comments

Many SAS programmers struggle with date manipulation, particularly when creating dynamic, reusable code. In a previous post, we covered converting a text…

Maximizing the Power of SAS Macro Variables: Using Them as Dates

Many SAS programmers struggle with date manipulation, particularly when creating dynamic, reusable code.

In a previous post, we covered converting a text string into a date variable, whether that string was hardcoded or stored in a variable. However, you could have a macro variable that looks like a date.

In this article, we assume the following before we convert a SAS macro variable to a date.

  1. You have a macro variable that looks like a date (e.g., 31DEC2020), and
  2. You want to create a new macro variable that contains the numeric representation of this date (e.g., 22280)
%let mydate = 2021-12-31;
%put original value: &mydate;
%let numeric_date= %sysfunc(inputn(&mydate, yymmdd10.));
%put numeric date : &numeric_date;
original value: 2021-12-31
 71         %let numeric_date= %sysfunc(inputn(&mydate, yymmdd10.));
 72         %put numeric date : &numeric_date;
 numeric date : 22645

You use SYSFUNC and INPUTN to change a macro variable into a date.

First, you must use SAS functions outside a SAS Data Step or PROC SQL with the SYSFUNC function. Then, you use the INPUTN function to turn the macro variable that looks like a date into an actual macro date.

To display the formatted SAS date in the log, you can use the

%let date_d9=%sysfunc(inputn(&mydate, yymmdd10.), date9.);
%put date9 format. : &date_d9;

%let date_d9=%sysfunc(inputn(2021-12-31, yymmdd10.), date9.);
%put date9 format. : &date_d9;

Note that the value passed in the input function should not be in quotes.

The INPUTN function is similar to the INPUT function because both convert text into numbers. However, you can use the INPUTN function in a %LET statement, whereas you can’t do this with the INPUT function.

The format of macro variables can be changed with the INPUTN, INPUTC or PUTN, PUTC functions.

To change a macro variable using numeric informat, use the INPUTN function.

To change a macro variable using a character format, use the PUTC function.

If you need to print it in a human-readable format in which case you can always use %SYSFUNC(PUTN(...))

Syntax:

val = %SYSFUNC(INPUTC(char val, informat));
val = %SYSFUNC(INPUTN(num val, informat));
val = %SYSFUNC(PUTC(char val, format));
val = %SYSFUNC(PUTC(num val, format))

Using macro dates as subsetting if OR WHERE statement

%let dt=01JAN2000;
data dtates2;
	set sashelp.rent;
	where date gt &DT;
run;

This throws an error because &dt is stored as a character.

Maximizing the Power of SAS Macro Variables: Using Them as Dates

The code works fine by adding a date literal in the macro variable.

%let dt='01JAN2000'D;
data dtates2;
	set sashelp.rent;
	where date gt &DT;
run;

Formatted SAS dates, which are text, must be enclosed in single or double quotes, followed by the letter D (since you are using a macro variable, it must be double quotes)

It is important to create a new variable for conversion. By using the same variables as before, variables will remain characters.

What if the macro variable contains a numeric representation of the date?

proc sql noprint;
select min(date) into :mindatel from
sashelp.buy;
quit;
%put &mindatel;
13149

The example below results in an error because the value of &mindate is a number. Even if you apply quotes, it results in an error.

proc sql;
select * from sashelp.rent
where date > &mindate;
quit;

proc sql;
select * from sashelp.rent
where date > "&mindate";
quit;
ERROR: Expression using greater than (>) has components that are of different data types.

Solution:

The solution is to add a date format to the macro variable while creating it and add the date literal while resolving the variable.

proc sql noprint;
select min(date) format = date9. into :mindate from
sashelp.buy;
quit;

proc sql;
select * from sashelp.rent
where date > "&mindate"D;
quit;
%put value is &mindate;

Conclusion

In conclusion, SAS macro variables offer a powerful and effective tool for manipulating dates in your code.

Using macro variables such as dates, you can greatly simplify your date manipulation processes and improve your code’s efficiency, flexibility, and reusability.

Whether you’re working with basic date calculations or more advanced techniques such as nested macro variables and array processing, SAS macro variables as dates provide a reliable and user-friendly solution.

We hope this post has given you a better understanding of the benefits and best practices for using SAS macro variables as dates and that you’ll be inspired to start incorporating them into your code.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #SAS Dates
Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to Run SAS Programs in Jupyter Notebooks with SASpy?
NextContinue
SAS Data Views: What They Are & How They Work

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You