Input data comes from the internal SASHELP.CLASS dataset, available by default in SAS.
1 Code Block
DATA STEP
Explanation : This DATA _NULL_ step iterates through the SASHELP.CLASS dataset. For each observation, CALL SYMPUTX is used to create a macro variable named 'STUDENT' followed by the observation number (_n_) and assign it the value of the 'name' variable. When the end of the file is reached (end=eof), a macro variable 'NUM_STUDENTS' is created, containing the total number of observations processed.
Copied!
data _null_;
set sashelp.class end=eof;
call symputx(cats('STUDENT',_n_),name);
if eof then call symputx('NUM_STUDENTS',_n_);
run;
1
DATA _null_;
2
SET sashelp.class END=eof;
3
call symputx(cats('STUDENT',_n_),name);
4
IF eof THEN call symputx('NUM_STUDENTS',_n_);
5
RUN;
2 Code Block
MACRO STATEMENT
Explanation : This block uses the %PUT statement to display the values of the previously created macro variables (NUM_STUDENTS and STUDENTx variables) directly in the SAS log. The NOSOURCE and SOURCE options are used to hide or show the SAS code itself in the log, respectively, allowing the results of the %PUT statements to be highlighted.
Copied!
* Verify the contents of the new macro variables by printing to the SAS log.;
options nosource;
%put ======================;
%put Number of Students: &NUM_STUDENTS;
%put;
%put Student 1: &STUDENT1;
%put Student 2: &STUDENT2;
%put Student 3: &STUDENT3;
%put ...;
%put Student 17: &STUDENT17;
%put Student 18: &STUDENT18;
%put Student 19: &STUDENT19;
%put ======================;
options source;
1
* Verify the contents of the new macro variables by printing to the SAS log.;
2
options nosource;
3
%put ======================;
4
%put Number of Students: &NUM_STUDENTS;
5
%put;
6
%put Student 1: &STUDENT1;
7
%put Student 2: &STUDENT2;
8
%put Student 3: &STUDENT3;
9
%put ...;
10
%put Student 17: &STUDENT17;
11
%put Student 18: &STUDENT18;
12
%put Student 19: &STUDENT19;
13
%put ======================;
14
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.
Copyright Info : HANDS-ON WORKSHOP, Title: Using SAS Macro Variable Lists to Create Dynamic Data-Driven Programs, Instructor: Josh Horstman
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.