Dear
Thank you for the suggestions,
Using a normal macro was a partial solution, but I was able to solve it entirely using a post from Reeza (maybe the same @Reeza of this thread) in StackOverflow (https://stackoverflow.com/questions/37017789/sas-creating-plots-for-all-variables).
I just made some additions to do the bar graphs and export as EMF, please see below (with some labels in portuguese).
Thank you very much, especially to Reeza,
Best wishes
ods graphics on/reset imagefmt=EMF;
ods rtf file="graficoshistogramas.rtf" path="your path here"
style=sty_custom;
proc sql noprint;
select name into :hist_state1-
from dictionary.columns
where upper(libname)='WORK'
and upper(memname)='MERGE0'
and type='num';
quit;
%let nobs=&sqlobs;
%macro generate_histogram;
%do i=1 %to &nobs;
proc sgplot data=WORK.MERGE0;
title "Histograma de &&&hist_state&i";
histogram &&&hist_state&i;
run;quit;
%end;
%mend;
%generate_histogram;
ods graphics on/reset imagefmt=EMF;
ods rtf file="graficosbarrafreq.rtf" path="your path here"
style=sty_custom;
proc sql noprint;
select name into :hist_state1-
from dictionary.columns
where upper(libname)='WORK'
and upper(memname)='MERGE0'
and type='char';
quit;
%let nobs=&sqlobs;
%macro generate_bar;
%do i=1 %to &nobs;
proc sgplot data=WORK.MERGE0;
title "Grafico de barras de &&&hist_state&i";
vbar &&&hist_state&i /datalabel;
xaxis display=(noline noticks);
yaxis display=(noline) grid label="contagem";
run;quit;
%end;
%mend;
%generate_bar;
Hello,
You need dynamic and generic code that is adapted depending on the variables that it finds in your databases.
You should use the dictionary tables and you need a program that writes another program (and the latter should be submitted).
In SAS, there are many techniques to avoid 'hard-coding' things and to generate dynamic code.
You can use macro's or you can use data-driven code generation with FILE statement & PUT stmt. & %INCLUDE or you can use CALL EXECUTE.
I have not read below article, but I think it deals with exactly this.
Have a look :
Data-Driven Programming Techniques Using SAS®
Posted 03-15-2021 09:43 AM | by KirkLafler
https://communities.sas.com/t5/SAS-Global-Forum-Proceedings/Data-Driven-Programming-Techniques-Using...
Good luck,
Koen
Dear
Thank you for the suggestions,
Using a normal macro was a partial solution, but I was able to solve it entirely using a post from Reeza (maybe the same @Reeza of this thread) in StackOverflow (https://stackoverflow.com/questions/37017789/sas-creating-plots-for-all-variables).
I just made some additions to do the bar graphs and export as EMF, please see below (with some labels in portuguese).
Thank you very much, especially to Reeza,
Best wishes
ods graphics on/reset imagefmt=EMF;
ods rtf file="graficoshistogramas.rtf" path="your path here"
style=sty_custom;
proc sql noprint;
select name into :hist_state1-
from dictionary.columns
where upper(libname)='WORK'
and upper(memname)='MERGE0'
and type='num';
quit;
%let nobs=&sqlobs;
%macro generate_histogram;
%do i=1 %to &nobs;
proc sgplot data=WORK.MERGE0;
title "Histograma de &&&hist_state&i";
histogram &&&hist_state&i;
run;quit;
%end;
%mend;
%generate_histogram;
ods graphics on/reset imagefmt=EMF;
ods rtf file="graficosbarrafreq.rtf" path="your path here"
style=sty_custom;
proc sql noprint;
select name into :hist_state1-
from dictionary.columns
where upper(libname)='WORK'
and upper(memname)='MERGE0'
and type='char';
quit;
%let nobs=&sqlobs;
%macro generate_bar;
%do i=1 %to &nobs;
proc sgplot data=WORK.MERGE0;
title "Grafico de barras de &&&hist_state&i";
vbar &&&hist_state&i /datalabel;
xaxis display=(noline noticks);
yaxis display=(noline) grid label="contagem";
run;quit;
%end;
%mend;
%generate_bar;
FYI - start by getting a program that works for a single variable and then generalize it using the tutorial below.
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
With RTF output to create EMF graphic output you would set
ods graphics /outputfmt=emf;
Typically I set all the graphic options before the ODS destination statement.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.