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!
data _null_;
set sashelp.class end=eof;
length student_list $200;
retain student_list;
student_list = catx('~',student_list,name);
if eof then do;
call symputx('STUDENT_LIST',student_list);
call symputx('NUM_STUDENTS',_n_);
end;
run;
1
DATA _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 THENDO;
7
call symputx('STUDENT_LIST',student_list);
8
call symputx('NUM_STUDENTS',_n_);
9
END;
10
RUN;
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!
options nosource;
%put ======================;
%put Number of Students: &NUM_STUDENTS;
%put Student List: &STUDENT_LIST;
%put ======================;
options source;
1
options nosource;
2
%put ======================;
3
%put Number of Students: &NUM_STUDENTS;
4
%put Student List: &STUDENT_LIST;
5
%put ======================;
6
options 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.
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.