BookmarkSubscribeRSS Feed
Calcite | Level 5

Hey everyone, I could use some help with a query. Im using sas studio but without much knowledge from sql. I can read and understand what it does, thats about it.

My query contains several filters with the same date, so in my previous program sas eg I used a prompt to set this date once, and it would be used throughout the entire flow. If i wanted to change this date I could just adjust this value instead of going through the set finding all the date filters.

Now someone told me I can do about the same in sas studio, by creating a program that says:

%global DateFrom %let DateFrom = ‘01nov2022’

And in the queries I could just filter on the name Datefrom.

I also tried &Datefrom because this is what I used in sas eg.

This isn’t working at the moment, could someone guide me in the right direction. Preferably by using the dropdown menu’s as filter, not by using code.

Thanks in advance!

Diamond | Level 26

Note the syntax corrections here and the straight quotes rather than curly quotes:


%global DateFrom;
%let DateFrom = '01nov2022'd;


But of course, it would really help to see where and how you intend to use the DATEFROM macro variable. Please show us.

Paige Miller
Super User Tom
Super User

You did not include the semicolons to end your statements. So your code should look like:

%global DateFrom;
%let DateFrom = '01nov2022' ;

You don't really need the first one since you are NOT running the code inside a macro.

If you want to use &DATEFROM as an actual DATE value then you should probably set the value to a date literal instead of character literal.

%let DateFrom = '01nov2022'd ;

Let's make some sample data we can try it out on.

data example;
  do week=0 to 10;
 format date date9.;


Now we can use the macro variable in a WHERE statement (that is an easy way to "filter" in SAS code).

proc print data=example;
  where date > &datefrom;


We can do the same thing using the filter open when browsing a dataset in SAS/Studio



Jade | Level 19

I would discourage using 

 %let DateFrom = ‘01nov2022’;

for macrovar datefrom.  Instead consider dropping the quotes, as in

 %let DateFrom = 01nov2022 ;

Then you can use a filter such as

where date >= "&datefrom"d 

Date literals like '01nov2022'd and "01nov2022"d have identical underlying date values.   But using single quotes with a macrovariable inside the quotes (as in '&datefrom'd) would prevent the macro processor from resolving &datefrom to 01nov2022, while double quotes (as in "&datefrom"d) will support resolution of the macrovar DATEFROM.

The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets




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. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1 like
  • 4 in conversation