BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

User define two parameters : 

%let start=1501; /*Jan 2015*/

%let end=1807;/*July 2018*/

The task is to find all quarters (in form YYMM) between start and end .

In this example  we need to get:

1503 (t1)

1506 (t2)

1509 (t3)

1512 (t4)

1603 (t5)

1606 (t6)

1609 (t7)

1612 (t8)

1703 (t9)

1706 (t10)

1709 (t11)

1712 (t12)

1803 (t13)

1806 (t14)

May anyone suggest how to do it please?

 

I wrote this code but I didn't get the correct answer

%let start=1501;
%let end=1807;


%let date_start=%sysfunc(inputn(&start.,yymmn4.));
%let date_end=%sysfunc(inputn(&end.,yymmn4.));
%put &date_start.;
%put &date_end.;

 

/*sas number of quarters between two dates*/
Data _null_;
no_of_quarters=intck('QUARTER', &date_start., &date_end.);
Call Symput("n",no_of_quarters);
/*call symput("n",trim(left(no_of_quarters)));*/
Run;
%put &n.;

%macro months;
%do j=1 %to &n.;
t&j.=put(intnx('QUARTER',&date_start.,&j.),yymmn4.);
call symput("t&j",trim(left(t&j.)));
%end;
%mend;

data _null_;
%months;
run;

4 REPLIES 4
mkeintz
PROC Star

It's probably a lot neater to do the work in a data _null_ step and use call symput to establish the macrovars:

 

%let b=1501;
%let e=1807;

data _null_;
  bdate=mdy(mod(&b,100),1,floor(&b/100));
  edate=mdy(mod(&e,100),1,floor(&e/100));
  do date=intnx('qtr',bdate,0,'end') by 0 while (date<edate);
    t=sum(t,1);
    call symput(cats('T',t),trim(put(date,yymmn4.)));
    date=intnx('qtr',date,1,'end');
  end;
run;
%put _user_;
--------------------------
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

--------------------------
Ronein
Meteorite | Level 14

Sorry,Maybe I didn't explain it well

We need to create parameters which are called  t1  ,t2,t3..etc.

They will get following values

t1=1503

t2=1506

t3=1509

t4=1512

....

t14=1806

 

 

PaigeMiller
Diamond | Level 26

The code provided by @mkeintz seems to do what you want. If it does not provide what you want, explain further. What happens when you run the code? Do you get an error? Does it produce the wrong results? Explain!


Also, from now on, when you are showing us your code, please click on the running man icon and paste your code into the window that opens.

 

 

--
Paige Miller
Tom
Super User Tom
Super User

You don't explain how your attempt failed but it looks like your approach is nearly correct.  Your algorithm finds the start of the quarter. But it looks from your required output that you want to find the END of the PREVIOUS quarter.

 

It is probably easier to do it all in a data step instead of mixing data steps and macro.

%let start=1501; /*Jan 2015*/
%let end=1807;/*July 2018*/

data _null_;
  start = input("&start",yymmn.);
  end = input("&end",yymmn.);
  do t=1 to intck('qtr',start,end);
    length varname $32 value $10 ;
    varname = cats('T',t) ;
    value = put(intnx('qtr',start,t-1,'e'),yymmn4.) ;
    put varname= value= ;
    call symputx(varname,value);
  end;
run;

You could also do it all in macro code.  If you make a separate macro and want to use the generated macro variables then you need to make them GLOBAL so that they will continue to exist after the macro finishes.

%macro make_t(start,end);
%local t varname ;
%let start=%sysfunc(inputn(&start,yymmn));
%let end=%sysfunc(inputn(&end,yymmn));
%do t=1 %to %sysfunc(intck(qtr,&start,&end)) ;
  %let varname=T&t ;
  %global &varname ;
  %let &varname = %sysfunc(intnx(qtr,&start,&t-1,e),yymmn4) ;
  %put &varname = &&&varname ;
%end ;
%mend make_t ;

%let start=1501; /*Jan 2015*/
%let end=1807;/*July 2018*/
%make_t(&start,&end);

 

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
  • 4 replies
  • 1427 views
  • 1 like
  • 4 in conversation