The script defines a macro `%graph_stocks`. First, it uses `PROC SQL` to create a macro variable (`STOCK_LIST`) containing a list of all distinct values of the `stock` variable. Then, a `%DO` loop iterates over this list. In each iteration, it extracts a stock name, opens a new PDF file, generates a 'high-low' graph with `PROC SGPLOT` for the data of that stock, and then closes the PDF file. The objective is to produce one graph per stock in a fully automated manner.
Data Analysis
Type : SASHELP
The script exclusively uses the `stocks` table from the standard `SASHELP` library.
1 Code Block
PROC SQL Data
Explanation : This SQL block prepares the logic for the dynamic loop. It selects unique values of the 'stock' column from `sashelp.stocks` and consolidates them into a single macro variable, `STOCK_LIST`, using '~' as a separator. The total number of stocks is stored in `NUM_STOCKS` via the automatic macro variable `SQLOBS`.
Copied!
proc sql noprint;
select distinct stock into :STOCK_LIST separated by '~'
from sashelp.stocks;
%let NUM_STOCKS = &sqlobs;
quit;
1
PROC SQL noprint;
2
select distinct stock into :STOCK_LIST separated BY'~'
3
from sashelp.stocks;
4
%let NUM_STOCKS = &sqlobs;
5
QUIT;
2 Code Block
PROC SGPLOT
Explanation : This block is a macro loop that executes as many times as there are distinct stocks. For each stock, it opens an ODS PDF destination, uses the `%SCAN` function to extract the stock name, and then `PROC SGPLOT` generates a 'high-low' graph, filtering the data for that specific stock. Each graph is saved in a separate PDF file.
Copied!
%do I = 1 %to &NUM_STOCKS;
ods pdf file= "%scan(&STOCK_LIST,&I,~).pdf";
proc sgplot data=sashelp.stocks;
where stock = "%scan(&STOCK_LIST,&I,~)_quoted_string_literal_;
highlow x=date high=high low=low;
run;
ods pdf close;
%end;
1
%DO I = 1 %to &NUM_STOCKS;
2
ods pdf file= "%scan(&STOCK_LIST,&I,~).pdf";
3
PROC SGPLOTDATA=sashelp.stocks;
4
where stock = "%scan(&STOCK_LIST,&I,~)_quoted_string_literal_;
5
highlow x=date high=high low=low;
6
RUN;
7
ods pdf close;
8
%END;
3 Code Block
Appel de Macro
Explanation : The entire code is encapsulated within a `%graph_stocks` macro for modularity. The `%graph_stocks;` call at the end of the script triggers the execution of the entire report generation process.
Copied!
%macro graph_stocks;
* Create the horizontal macro variable list.;
proc sql noprint;
select distinct stock into :STOCK_LIST separated by '~'
from sashelp.stocks;
%let NUM_STOCKS = &sqlobs;
quit;
%do I = 1 %to &NUM_STOCKS;
ods pdf file= "%scan(&STOCK_LIST,&I,~).pdf";
proc sgplot data=sashelp.stocks;
where stock = "%scan(&STOCK_LIST,&I,~)__quoted_string_literal__;
highlow x=date high=high low=low;
run;
ods pdf close;
%end;
%mend graph_stocks;
%graph_stocks;
1
%macro graph_stocks;
2
3
* Create the horizontal macro variable list.;
4
PROC SQL noprint;
5
select distinct stock into :STOCK_LIST separated BY'~'
6
from sashelp.stocks;
7
%let NUM_STOCKS = &sqlobs;
8
QUIT;
9
10
%DO I = 1 %to &NUM_STOCKS;
11
ods pdf file= "%scan(&STOCK_LIST,&I,~).pdf";
12
PROC SGPLOTDATA=sashelp.stocks;
13
where stock = "%scan(&STOCK_LIST,&I,~)__quoted_string_literal__;
14
highlow x=date high=high low=low;
15
RUN;
16
ods pdf close;
17
%END;
18
19
%mend graph_stocks;
20
21
%graph_stocks;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.