BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
random1
Fluorite | Level 6

 

I am trying to subset my dataset based on months, and I want it to loop through the months and create a different dataset for each month.

This is my code:

%macro SASPLay;

%let start = 30JUN2014;

%do i=0 %to 10;

    data _null_;

  call symputx("mth",put(intnx("MONTHS",input("&start",Date9.),&i,'SAME'),Date9.));

     run;

  %put &mth;

 

    data final_&mth;

    set mockdata;

  where DATE = '&mth'd;

run;

%end;

%mend;

 

%SASPlay;

 

It gives these error messages:

ERROR: Invalid date/time/datetime constant '&mth'd.

ERROR: Syntax error while parsing WHERE clause.

 

 

I am trying to generalise this, because when I use a hardcoded value (e.g.  where DATE = '30JUN2014'd; ) it works.

I don't know why I can't just swap out the hardcoded value for &mth, giving that &mth shows as 30JUN2014.

I also tried just using where DATE = &mth; That doesn't work either.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
random1
Fluorite | Level 6

Ohh, looks like I got it to work with

 

where DATE = "&mth"d;

 

double quotes, not single quotes.

**bleep** this is strange.

 

View solution in original post

3 REPLIES 3
random1
Fluorite | Level 6

Ohh, looks like I got it to work with

 

where DATE = "&mth"d;

 

double quotes, not single quotes.

**bleep** this is strange.

 

ballardw
Super User

@random1 wrote:

Ohh, looks like I got it to work with

 

where DATE = "&mth"d;

 

double quotes, not single quotes.

**bleep** this is strange.

 


Not at all strange. The use of double quotes for macro variables to resolve is very well documented. For one thing it allows the use of literal text values containing possible macro invocations such as '%InData' or '&Sons' without the macro processor trying to handle such when you know they are not macro code or macro variables.

PGStats
Opal | Level 21

macro variable replacement doesn't occur within single quotes. Use double quotes instead

 

where DATE = "&mth"d;

PG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1748 views
  • 0 likes
  • 3 in conversation