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
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
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.