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

I am trying to use a string that is saved in a metadata file in order to dynamically create headers for PROC REPORT. I use this code snippet to create a small data set with all of the words to be used as column headers.

data defines;
	length word $100;
	drop string;
	DO UNTIL(word="");
		count+1;
		word=scan("&xcols", count, ".");
		output;
		end;
run;

Next I run this code to get the min and max number to iterate through for the headers. 

PROC SQL noprint;
	SELECT min(count), max(count)
	INTO :xmin, :xmax
	FROM defines;
quit;

Then this is where I am getting stuck. I need to know how to define the columns correctly as well as display the correct column header. For example the first column header would be the first observation in the defines data set, second header would be the second obs in the data set and so on. I have also included the dummy data set creation here. 

data dummy;
length F1-F10 $100;
run;

proc report data=dummy nowd COLUMNS F1-F10; %DO r=&xmin %TO &xmax; DEFINE VVALUEX("F"||&r) DISPLAY "TEST"; %END; run;

I am open to a more efficient solution to this problem as well. Any feedback helps! Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Example using phony data in data set HAVE

 

data have;
    set sashelp.class(rename=(age=f1 height=f2 weight=f3));
run;

%let xcols=Age.Height.Weight;
%macro report;
proc report data=have;
    columns f1-f3;
    %do i=1 %to %sysfunc(countw(&xcols,.));
        %let thiscol=%scan(&xcols,&i,.);
        define f&i/display "&thiscol";
    %end;
run;
%mend;
%report

  

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

Example using phony data in data set HAVE

 

data have;
    set sashelp.class(rename=(age=f1 height=f2 weight=f3));
run;

%let xcols=Age.Height.Weight;
%macro report;
proc report data=have;
    columns f1-f3;
    %do i=1 %to %sysfunc(countw(&xcols,.));
        %let thiscol=%scan(&xcols,&i,.);
        define f&i/display "&thiscol";
    %end;
run;
%mend;
%report

  

--
Paige Miller

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
  • 1 reply
  • 1040 views
  • 1 like
  • 2 in conversation