SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Create all possible combination out of dynamic length variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Create all possible combination out of dynamic length variables

[ Edited ]

I have three macro variables that contai some dynamic values - the number of values or the values themselves may change in the script flow.

 

 

 

%let x = 1 2 5;
%let y = 10 15;
%let z = 5 15 25 60 80;

 

 

I need to create all possible combinations out of these variables and save the data - it would contain 3*2*5 = 30 rows. I looked at proc plan and it doesn't seem to ba able to do this, not directly at least. Also tried this:

 

data GridSearch;
do a = SYMGET('x');
do b = SYMGET('y');
do c = SYMGET('z');
output;
end;
end;
end;
run;

Apparently this is not the correct way to reference maro varaible as it returns the as text rather than lsit of values. 

 

 

Any ideas?

 

 


Accepted Solutions
Solution
‎12-21-2016 06:29 AM
Super User
Super User
Posts: 7,392

Re: Create all possible combination out of dynamic length variables

Well, to answer your question first, do loops need to, when a list of values is provided, be separated by commas not spaces:

%let x = 1,2,5;
%let y = 10,15;
%let z = 5,15,25,60,80;

data GridSearch;
 do a = &x.;
   do b = &y.;
     do c = &z.;
       output;
     end;
   end;
 end;
run;

Now, from my side, I would increment the indentation for each loop to make the code readable.

View solution in original post


All Replies
Solution
‎12-21-2016 06:29 AM
Super User
Super User
Posts: 7,392

Re: Create all possible combination out of dynamic length variables

Well, to answer your question first, do loops need to, when a list of values is provided, be separated by commas not spaces:

%let x = 1,2,5;
%let y = 10,15;
%let z = 5,15,25,60,80;

data GridSearch;
 do a = &x.;
   do b = &y.;
     do c = &z.;
       output;
     end;
   end;
 end;
run;

Now, from my side, I would increment the indentation for each loop to make the code readable.

Super User
Posts: 9,671

Re: Create all possible combination out of dynamic length variables


%let x = 1 2 5;
%let y = 10 15;
%let z = 5 15 25 60 80;

data want;
 do x=%sysfunc(translate(&x,%str(,),%str( )));
  do y=%sysfunc(translate(&y,%str(,),%str( )));
   do z=%sysfunc(translate(&z,%str(,),%str( )));
    output;
   end;
  end;
 end;
run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 338 views
  • 1 like
  • 3 in conversation