Published on :
Macro SASHELP

Using SAS Macro Variable Lists to Create Dynamic Data-Driven Programs

This code is also available in: Deutsch Español Français
This hands-on workshop (Exercise 02) aims to demonstrate how to create a dynamic macro variable from existing data. The objective is to extract the weight of an 'Acura MDX' from the SASHELP.CARS table using PROC SQL's INTO clause and store this value in a macro variable called MDX_WEIGHT. The content of the macro variable is then verified by displaying it in the SAS© log.
Data Analysis

Type : SASHELP


The input data comes from the SASHELP.CARS table, an integrated SAS library that contains information about different car makes and models.

1 Code Block
PROC SQL
Explanation :
This block uses the SQL procedure to query the SASHELP.CARS table. It selects the 'weight' column for all entries where 'make' is 'Acura' and 'model' is 'MDX'. The found value is then stored in a macro variable named MDX_WEIGHT. The `trimmed` option removes unnecessary whitespace, and `noprint` prevents the query output from being displayed in the output window.
Copied!
1PROC SQL noprint;
2 select weight into :MDX_WEIGHT trimmed
3 from sashelp.cars
4 where strip(make)='Acura' and strip(model)='MDX';
5QUIT;
2 Code Block
MACRO/OPTIONS
Explanation :
This code block is used to verify the successful creation of the macro variable. `options nosource` disables the display of SAS code in the log, while `options source` re-enables it. Between these two options, the `%put` macro is used to display a formatted character string in the SAS log, including the value of the MDX_WEIGHT macro variable, thereby confirming its value.
Copied!
1* Verify the contents of the new macro variable by printing to the SAS log.;
2options nosource;
3%put ===================================;
4%put The weight of an Acura MDX is &MDX_WEIGHT..;
5%put ===================================;
6options SOURCE;
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.
Copyright Info : HANDS-ON WORKSHOP, Title: Using SAS Macro Variable Lists to Create Dynamic Data-Driven Programs, Instructor: Josh Horstman, Exercise: 02