BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HermanoRocha
Fluorite | Level 6
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
Fluorite | Level 6

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
Fluorite | Level 6

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
Fluorite | Level 6

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1250 views
  • 5 likes
  • 4 in conversation