Manipulating Dataset by Quarters Instead of Dates

Reply
New Contributor
Posts: 3

Manipulating Dataset by Quarters Instead of Dates

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? 

Super User
Posts: 22,874

Re: Manipulating Dataset by Quarters Instead of Dates

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? 


 

New Contributor
Posts: 3

Re: Manipulating Dataset by Quarters Instead of Dates

My code for creating the quarter variable is: 

data want:

set have;

format  quarter yyqs8.;

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

run;

Super User
Posts: 22,874

Re: Manipulating Dataset by Quarters Instead of Dates

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;




 

 

Super User
Posts: 13,084

Re: Manipulating Dataset by Quarters Instead of Dates


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.

 

 

 

PROC Star
Posts: 2,231

Re: Manipulating Dataset by Quarters Instead of Dates

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.

 

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 99 views
  • 0 likes
  • 4 in conversation