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.
if
, not%if
. – Hong Ooiif
statement. That will be able to see data step variables like thefirst.
variables generated when you use aby
statement. – Tom%if
is for macro conditional logic to control what code your macro generates. You still useif
andwhere
for data conditional operations. Macros just generate SAS code, so they need to generate valid SAS code. – Tom