Desktop productivity for business analysts and programmers

Create a table using values from macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 79
Accepted Solution

Create a table using values from macro variables

Users define a couple of macro variables (input):

%let category = A B;
%let class = apple banana;
%let column = index character;

The generated table/dataset (output) would look like:

category class index character
   A     apple   .       .
   A     banana  .       .
   B     apple   .       .
   B     banana  .       .

As you can see, for column 1 and column 2, they basically go through values of the macro variables, with macro variables' names as the column names. For column 3 and column 4, blanks, with macro variable's value as the column names (column 'index' has numeric values; column 'character' has char.)

 

How to achieve it?


Accepted Solutions
Solution
‎03-29-2017 06:38 PM
Respected Advisor
Posts: 4,972

Re: Create a table using values from macro variables

[ Edited ]

Untested, so it's possible it needs a little debugging:

 

%macro generate (category=, class=, column=);

 

%local i j;

data want;

length category class $ 8 &column 8;

%do i=1 %to %sysfunc(countw(&category));

%do j=1 %to %sysfunc(countw(&class));

category = "%scan(&category, &i)";

class = "%scan(&class, &j)";

output;

%end;

%end;

run;

 

%mend generate;

 

%generate (category=A B, class=apple banana, column=index character)

View solution in original post


All Replies
Solution
‎03-29-2017 06:38 PM
Respected Advisor
Posts: 4,972

Re: Create a table using values from macro variables

[ Edited ]

Untested, so it's possible it needs a little debugging:

 

%macro generate (category=, class=, column=);

 

%local i j;

data want;

length category class $ 8 &column 8;

%do i=1 %to %sysfunc(countw(&category));

%do j=1 %to %sysfunc(countw(&class));

category = "%scan(&category, &i)";

class = "%scan(&class, &j)";

output;

%end;

%end;

run;

 

%mend generate;

 

%generate (category=A B, class=apple banana, column=index character)

☑ This topic is SOLVED.

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

Discussion stats
  • 1 reply
  • 159 views
  • 1 like
  • 2 in conversation