Published on :
Macro SASHELP

Dynamic creation of horizontal macro variable list

This code is also available in: Deutsch Español Français
This script illustrates how to create a horizontal list of values stored in a macro variable during a DATA step execution. It uses the SASHELP.CLASS table to concatenate all student names, separated by a tilde, and also stores the total number of students.
Data Analysis

Type : SASHELP


The data comes from the standard SASHELP.CLASS table.

1 Code Block
DATA STEP
Explanation :
This DATA _NULL_ step iterates through the SASHELP.CLASS table. The RETAIN statement and the CATX function allow accumulating names into the 'student_list' variable. At the last observation (eof), CALL SYMPUTX assigns the complete list and the number of observations (_n_) to macro variables.
Copied!
1DATA _null_;
2 SET sashelp.class END=eof;
3 LENGTH student_list $200;
4 retain student_list;
5 student_list = catx('~',student_list,name);
6 IF eof THEN DO;
7 call symputx('STUDENT_LIST',student_list);
8 call symputx('NUM_STUDENTS',_n_);
9 END;
10RUN;
2 Code Block
SAS MACRO LANGUAGE
Explanation :
Displays the content of the created macro variables (&NUM_STUDENTS and &STUDENT_LIST) in the SAS log for verification. The 'nosource' option is used to clean up the log output.
Copied!
1options nosource;
2%put ======================;
3%put Number of Students: &NUM_STUDENTS;
4%put Student List: &STUDENT_LIST;
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 : Instructor : Josh Horstman