0

The character variable in dataset never matches with the macro variable. The %IF loop never comes true. Kindly advice.

I am trying to match by months and accordingly trying to create array and put counts only for specific months. Not working because the month macro variable never matches with dataset variable having month.

/*create dummy data*/
  data datefile;
  input tran_date date9. cnt 3.;
  datalines;
 13feb2015 5
 10feb2015 4
 11feb2015 3
 05feb2015 8
 08feb2015 5
 01jan2015 1
 20dec2014 1
 31jan2015 2
 23dec2014 2
 12jan2015 1
   ;


 /*calculate month*/
data datefile11;
set datefile;
tran_mon=year(tran_date)*100+month(tran_date);
run;

/*select distinct month*/
proc sql;
create table datefile12 as select distinct(tran_mon)
from datefile11 order by tran_mon;
quit;

/*convert month from numeric to character*/
data datefile11(drop=tran_mon);
informat tran_mon2 $6.;
set datefile11;
tran_mon2=tran_mon;
run;

/*create macro variables through datastep*/
data datefile13;
set datefile12;
monum = cat('mnth',_N_);
run;


data _null_;
set datefile13;
call symput(monum,trim(left(tran_mon)));
run;



/*use array to make separate column for each month and 
  put split count for each month to each colunms*/
  %macro c;
  proc sql noprint;
  select count(1) into :nrow from datefile13;
  quit;

  %let nrow = &nrow;

  data datefile14;
  set datefile11;
  array mon{*} mon_1 - mon_&nrow;
  %do i=1 %to &nrow;
  %if tran_mon2 = &&mnth&i %then %do; %put tran_mon2; 
                                     mon_&i = cnt; %end;
  %else %do; mon_&i = 0 ; %end;
  %end;
  run;
  %mend c;

  %c
Joe
  • 62,789
  • 6
  • 49
  • 67
Rajat Panda
  • 129
  • 1
  • 3
  • 14

1 Answers1

2

Your macro %if %then %do check executes while the data step is still being compiled - by the time the data step has begun to execute, there is no further opportunity to use macro logic like that.

Try doing it the other way round - write your loop using if then do data step logic instead.

user667489
  • 9,501
  • 2
  • 24
  • 35