BookmarkSubscribeRSS Feed
pikardenz
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!

3 REPLIES 3
PaigeMiller
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
Tom
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;
    date=intnx('week','01OCT2022'd,week,'same');
    output;
  end;
 format date date9.;
run;

Tom_0-1683311664391.png

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;
run;

Tom_1-1683311735612.png

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

Tom_2-1683311805492.pngTom_3-1683311826105.png

 

mkeintz
PROC Star

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

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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