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

I want to be able to dynamically define a series of macro variables based on the value of another variable. The easiest example to describe this would be... say I have a list of reporting variables (A_rpt, B_rpt, C_rpt...) and I know what pages I want them to appear on when I create some ODS output. So imagine I have a data set like this:

RPT_Variable    Page

A_rpt                  1

B_rpt                  1

C_rpt                  1

D_rpt                  2

E_rpt                  3

F_rpt                  3

I want to create three different macro variable lists that would contain the following

List1 = A_rpt B_rpt C_rpt

List2 = D_rpt

List3 = E_rpt F_rpt

I can find lots of examples of how to put a single list into a macro variable (either using %let, or SQL), or how to create a series of macro variables using SQL (shown below), but any hints how to do something that is somewhere in between, either by modifying the code below or taken a different approach?

Thanks for any help you can provide

%macro rptlist;

proc sql noprint;

  select distinct rpt_variable into:rptvar1-:rptvar999 from set_my_page;

  select page into:page1-:page999 from set_my_page;

quit;

%do i = 1 %to&sqlobs;

    %put &&rpt_var&i &&page&i;

%end;

%mend;

%varlist;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

A standard data step works nicely as well.

DATA SAMPLE;

LENGTH VAR_LIST $256.;

SET HAVE;

BY PAGE;

RETAIN VAR_LIST;

IF FIRST.PAGE THEN VAR_LIST=RPT_VARIABLE;

ELSE VAR_LIST=CATX(" ", VAR_LIST, RPT_VARIABLE);

TEST=FIRST.PAGE;

IF LAST.PAGE THEN DO;

    CALL SYMPUTX("PAGE"||PUT(PAGE, 2. -L), VAR_LIST);

END;

RUN;

%PUT &PAGE1.;

%PUT &PAGE2.;

%PUT &PAGE3.;

%PUT &PAGE4.;

View solution in original post

3 REPLIES 3
pradeepalankar
Obsidian | Level 7

hi try this:

data test;

input  RPT_Variable $ page;

cards;

A_rpt 1

B_rpt 1

C_rpt 1

D_rpt 2

E_rpt 3

F_rpt 3

;

proc sql noprint;

select distinct page into :pages separated by '|' from test;

quit;

%macro test;

%PUT &PAGES;

%do i= 1 %to %sysfunc(countw(&pages,'|'));

%let list=%scan(&pages,&i,'|');

proc sql noprint;

select RPT_Variable into :list_&list separated by ' ' from test

where page=&list;

quit;

%put  list_&list-->&&list_&list;

%end;

%mend;

%test;

output:

list_1-->A_rpt B_rpt C_rpt

list_2-->D_rpt

list_3-->E_rpt F_rpt

Reeza
Super User

A standard data step works nicely as well.

DATA SAMPLE;

LENGTH VAR_LIST $256.;

SET HAVE;

BY PAGE;

RETAIN VAR_LIST;

IF FIRST.PAGE THEN VAR_LIST=RPT_VARIABLE;

ELSE VAR_LIST=CATX(" ", VAR_LIST, RPT_VARIABLE);

TEST=FIRST.PAGE;

IF LAST.PAGE THEN DO;

    CALL SYMPUTX("PAGE"||PUT(PAGE, 2. -L), VAR_LIST);

END;

RUN;

%PUT &PAGE1.;

%PUT &PAGE2.;

%PUT &PAGE3.;

%PUT &PAGE4.;

sgnolek
Obsidian | Level 7

Thanks to both of you for very helpful answers that worked great. Much appreciated!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1429 views
  • 3 likes
  • 3 in conversation