BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sburnos
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Satish_Parida
Lapis Lazuli | Level 10

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.

View solution in original post

7 REPLIES 7
sburnos
Fluorite | Level 6

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. 

Reeza
Super User

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. 


 

Astounding
PROC Star
It's mildly awkward but you can break up the data step into 2 steps. After call symputx, insert into the program:

stop;
run;

At that point you have the macro variable and can repeat the beginning of the data step:

Data
Set
Array
Array
%do
andreas_lds
Jade | Level 19
Maybe I am short-sighted, but why do you need the dimension in a macro-variable? Why not just query sashelp.vcolumn and create a macro variable with the names of the variables?
ErikLund_Jensen
Rhodochrosite | Level 12

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.

 

 

 

 

 

 

 

Satish_Parida
Lapis Lazuli | Level 10

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 940 views
  • 2 likes
  • 7 in conversation