Dear All,
could you please suggest a solution for the following issue, I cannot save the dim(ArrayName) into a macro variable in order to use it later:
data test;
var_test___1=0;
var_test___2=1;
var_test___3=1;
var_test___4=1;
var_test___5=0;
var_test___6=0;
run;
%macro instrument_inarow(var= );
%local j var_length;
data output_data;
set test(keep=&var.: );
array list_asc(*) &var.:;
call symputx('var_length',dim(list_asc),'G'); * HERE is the issue, I need to assign to var_length = dim(list_asc)*/
array list_desc(*)
%do j=&var_length. %to 1 %by -1;
&var.___&j.
%end;
%str(; );
if 1 then flg=1; /*here comes some conditions*/
else flg=0;
run;
%mend;
%instrument_inarow(var=var_test);
What would be a solution?
Many thanks in advance!
You can not use a macro variable created in same datastep.
Re-Written Code:
data test;
var_test___1=0;
var_test___2=1;
var_test___3=1;
var_test___4=1;
var_test___5=0;
var_test___6=0;
run;
option mprint mlogic symbolgen source source2;
%macro instrument_inarow(var= );
%local j var_length;
data _null_;
set test(keep=&var.: );
array list_asc(*) &var.:;
call symput('var_length',dim(list_asc));
stop;
run;
data output_data;
set test(keep=&var.: );
array list_desc(*)
%do j=&var_length. %to 1 %by -1;
&var.___&j.
%end;
%str(; );
if 1 then flg=1;
else flg=0;
run;
%mend instrument_inarow;
%instrument_inarow(var=var_test);
Please let us know if it worked.
You cannot use a macro variable in the same step where it is created. To count the number of items in &var, use
%sysfunc(countw(&var.))
Thanks for the reply.
Unfortunately, this does not work as I need to count the dimension of the array.
If I use the
%sysfunc(countw(&var.))
the result of the length_var =1, but I need it to be 6.
Use SYMGET then to get the macro variable or separate it into two steps.
@sburnos wrote:
Thanks for the reply.
Unfortunately, this does not work as I need to count the dimension of the array.
If I use the
%sysfunc(countw(&var.))
the result of the length_var =1, but I need it to be 6.
Hi @sburnos
I wonder why you are doing this. You are building a new array with the same variables in reverse order. It does not affect the order of variables in your output, so I guess it serves some purpose in your real code, where you have a dummy condition in your example.
You have the dimension available in the data step, so it might be possible to do the same thing on the original array by using 2 index variables, one going from 1 to dim, and another from dim to 1. By the way, do you have more than one record in real data? - in that case your array will always contain the full set of variables, whether missing in the actual record or not.
You can not use a macro variable created in same datastep.
Re-Written Code:
data test;
var_test___1=0;
var_test___2=1;
var_test___3=1;
var_test___4=1;
var_test___5=0;
var_test___6=0;
run;
option mprint mlogic symbolgen source source2;
%macro instrument_inarow(var= );
%local j var_length;
data _null_;
set test(keep=&var.: );
array list_asc(*) &var.:;
call symput('var_length',dim(list_asc));
stop;
run;
data output_data;
set test(keep=&var.: );
array list_desc(*)
%do j=&var_length. %to 1 %by -1;
&var.___&j.
%end;
%str(; );
if 1 then flg=1;
else flg=0;
run;
%mend instrument_inarow;
%instrument_inarow(var=var_test);
Please let us know if it worked.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.