I am trying to build bins for numeric variables. Everything worked fine when I haven't used %macro therefore I believe it's something macro specific. Basically this part of code:
%if first.&rank_column. = 1 %then %do;
doesn't work as I anticipated. Below I simplified to just one iteration and one variable but my actual code have many more - however the problem is the same.
Full code:
/* Build binns */
%let var_list = MSRP Invoice;
%macro group_var_in_bins(source_table);
data WITH_BINNS;
set &source_table.;
run;
%let loop_count = %sysfunc(countw(&var_list));
%do i=1 %to &loop_count.;
* list of variables to use;
%let variable = %scan(&var_list, &i);
%let rank_column = &variable._bins_rank;
proc rank data=WITH_BINNS out=WITH_BINNS groups=10 ties=low;
var &variable.;
ranks &rank_column.;
run;
* sort the table by this variable;
proc sort data=WITH_BINNS out=WITH_BINNS;
by &variable.;
run;
* Build start and end observation of particular bin;
data WITH_BINNS;
set WITH_BINNS;
by &rank_column.;
*this is just to check if first.&rank_column works;
first_&rank_column. = first.&rank_column.;
last_&rank_column. = last.&rank_column.;
%if first.&rank_column. = 1 %then %do;
/* here %if first.&rank_column. %then %do erros so something is wrong with argument statement*/
Start_bin = &variable.;
%end;
%else %do;
Start_bin = .;
%end;
%if last.&rank_column. = 1 %then %do;
End_bin = &variable.;
%end;
%else %do;
End_bin = .;
%end;
run;
%end;
* some more code which amends WITH_BINNS table;
%mend group_var_in_bins;
%group_var_in_bins(sashelp.cars);
so the loop doesn't recognize argument in %if part.
Thanks for help!!
[Edit]:
To clarify, steps I want to do are:
for variable in list &var_list.
build a rank for it
sort by that variable
using data step by: group by rank
find the value of this variable which corresponds to beggining of group using first. and end end of group using last. leave the rest empty
some next steps...
So basically I want to create begging and end of rank interval.
I example from picture; the first row has first. = 1 therefore Start_bin should e $10.280 and End_bin should be empty. The next row should be empty because both first. and last. are 0.
So your basic problem is you are using macro logic where you should be using normal logic.
%if first.&rank_column. = 1 %then %do;
Will never be true, even if rank_column
is empty because the string first.
can never equal the string 1
.
But if you code it using SAS code instead of macro code
if first.&rank_column. = 1 then do;
Then it will be true when you are on the first observation in that particular value of the variable named by the value of the macro variable rank_column
.
You probably have bigger problems with your overall logic because you are overwriting the same variable names start_bin
and end_bin
in the same dataset. So only the values for the bins generated by the last variable your list will be available after the macro has finished.