BookmarkSubscribeRSS Feed
Sam_I_am
Calcite | Level 5

I'm interested in creating macro variables for quarters such as

%let start=2013/1;
%let end=2014/2;

 

I already have numeric quarters assigned in the format above (the format is not relevant, 2012/1 is just what I chose). But when I try to limit my dataset to these quarters, I don't get the output I am looking for. Here is an example of my code: 

data want;
  set have;

  if &start.<=quarter<=&end. then do;

    a=1; 
  end;

run;

The column for "a" ends up being blank. Any suggestions on how to fix this issue? 

5 REPLIES 5
Reeza
Super User

Yeah, you need to have them in the same format as your variables. So whats the type and format of your variable? And what do they look like? Basically what does working code look like without macro variables. That's your starting point. 

 


@Sam_I_am wrote:

I'm interested in creating macro variables for quarters such as

%let start=2013/1;
%let end=2014/2;

 

I already have numeric quarters assigned in the format above (the format is not relevant, 2012/1 is just what I chose). But when I try to limit my dataset to these quarters, I don't get the output I am looking for. Here is an example of my code: 

data want;
  set have;

  if &start.<=quarter<=&end. then do;

    a=1; 
  end;

run;

The column for "a" ends up being blank. Any suggestions on how to fix this issue? 


 

Sam_I_am
Calcite | Level 5

My code for creating the quarter variable is: 

data want:

set have;

format  quarter yyqs8.;

quart= qtr(date);
quarter = yyq(year, quart);

run;

Reeza
Super User

YYQ returns a SAS Date, which means you need a SAS date for comparison, not '2017/1' 

 

My question remains, how do you do this without macro variables? Once you have that working, you can convert it to use macro variables. 

 


@Sam_I_am wrote:

My code for creating the quarter variable is: 

data want:

set have;

format  quarter yyqs8.;

quart= qtr(date);
quarter = yyq(year, quart);

run;




 

 

ballardw
Super User

@Sam_I_am wrote:

My code for creating the quarter variable is: 

data want:

set have;

format  quarter yyqs8.;

quart= qtr(date);
quarter = yyq(year, quart);

run;


And how exactly is that quarter variable used? If you are comparing to a date then a date variable will be preferable (in general). You can use date functions to "match" such as if qtr(thisdate) = qtr(thatdate) or increment/determine number of quarter intervals between dates with INTNX or INTCK functions.

 

Your code isn't selecting quarters because of the implied arithmetic in your macro variables. Please see:

%let start=2013/1;
%let end=2014/2;
data junk;
 x = &start;
 y = &end;
run;
proc print data=junk;
run; 

If you want to compare a  date then use a date literal in the proper form:

 

%let start='01Jan2013'd;
%let end='30Jun2014'd;

But @Reeza is correct: Get the code working without macro variables first.

 

 

 

ChrisNZ
Tourmaline | Level 20

the format is not relevant

Not too sure what this means.

 

%let end=2014/2;

 

Your test resolves to

  if 2013 <= QUARTER <= 1007

which always fails.

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1859 views
  • 0 likes
  • 4 in conversation