BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HermanoRocha
Obsidian | Level 7
Dear
I perform a repetitive task which is to generate univariate graphs of all variables from different databases weekly, bar graphs for categoricals and histograms for numeric ones, with publication formatting (apa), and export as emf to a RTF file, because I need the vector format. currently I have to create the graphs one by one, manually (banks with a few hundred variables). Does anyone have any suggestions on how I could try to automate this activity in sas 9.4?
Thank you very much for the attention of the community.
1 ACCEPTED SOLUTION

Accepted Solutions
HermanoRocha
Obsidian | Level 7

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;

View solution in original post

7 REPLIES 7
sbxkoenk
SAS Super FREQ

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

HermanoRocha
Obsidian | Level 7

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;
Reeza
Super User
Yup, that's my solution from SO....5 years ago 😄
HermanoRocha
Obsidian | Level 7

Thank you @Reeza, I use tablen macro a lot, I wish now to complement with graphs,

 

Best wishes

Reeza
Super User

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...

ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 1670 views
  • 5 likes
  • 4 in conversation