The attributes of variables for each dataset is defined in excel sheet. How to generate a macro with macro name of the table and create the following,(less hardcoding)
Table_Name Column_Name Data_Type Length TS1 STUDYID NUMBER 10 TS1 SUBJID NUMBER 10 TS1 AETERM VARCHAR 200 TS2 STUDYID NUMBER 10 TS2 PROJID NUMBER 10 TS2 AGE NUMBER 10 %macro ts1; attrib _trgt_STUDYID length=8; attrib _trgt_SUBJID length=8; atrrib _trgt_AETERM length=$200; %mend ts1; %macro ts2; attrib _trgt_STUDYID length=8; attrib _trgt_PROJID length=8; atrrib _trgt_AGE length=8; %mend ts2;
Not sure that generating macros adds anything, but it is not hard to generate code from data.
The PUT statement is a really good way.
For your example you also need logic to convert your metadata (which appears to be for some other database format) into metadata that is valid for SAS datasets. So if you have this existing Metadata:
data rdb_metadata ;
length Table_Name $32 Column_Name $32 Data_Type $15 Length 8;
input Table_Name -- Length ;
cards;
TS1 STUDYID NUMBER 10
TS1 SUBJID NUMBER 10
TS1 AETERM VARCHAR 200
TS2 STUDYID NUMBER 10
TS2 PROJID NUMBER 10
TS2 AGE NUMBER 10
;
You could convert it to metadata that would work for SAS
data sas_metadata ;
length Memname $32 Varnam 8 Name $32 Type $4 Length $6 Length_num 8 Format $41 Informat $41 ;
set dba_metadata (rename=(length=length_num));
by Table_Name ;
varnum+1;
if first.table_name then varnum=1;
Memname = Table_Name ;
Name = Column_Name
if Data_Type in ('CHAR','VARCHAR') then do;
type = 'char';
length = cats('$',length_num);
end;
if Date_Type='DATE' then do;
type='num';
length='8';
length_num=8;
format='DATE9.';
informat='DATE9.';
end;
else do;
type='num';
format = cats('F',length_num,'.');
length='8';
length_num=8;
end;
run;
Now that you have it you can use it to write some SAS code.
For example to create you macro variables that are named after the tables.
filename code temp;
data _null_;
set sas_metadata ;
by memname varnum ;
file code ;
if first.memname then put '%macro ' memname ';' ;
put 'attrib ' name length= @;
if not missing(format) then put format= @;
if not missing(informat) then put informat= @;
put ';' ;
if last.memname then put '%mend ' memname ';' ;
run;
You could then %INCLUDE the generated code file to define the macros.
Lexjansen has some demos on this.
Not sure that generating macros adds anything, but it is not hard to generate code from data.
The PUT statement is a really good way.
For your example you also need logic to convert your metadata (which appears to be for some other database format) into metadata that is valid for SAS datasets. So if you have this existing Metadata:
data rdb_metadata ;
length Table_Name $32 Column_Name $32 Data_Type $15 Length 8;
input Table_Name -- Length ;
cards;
TS1 STUDYID NUMBER 10
TS1 SUBJID NUMBER 10
TS1 AETERM VARCHAR 200
TS2 STUDYID NUMBER 10
TS2 PROJID NUMBER 10
TS2 AGE NUMBER 10
;
You could convert it to metadata that would work for SAS
data sas_metadata ;
length Memname $32 Varnam 8 Name $32 Type $4 Length $6 Length_num 8 Format $41 Informat $41 ;
set dba_metadata (rename=(length=length_num));
by Table_Name ;
varnum+1;
if first.table_name then varnum=1;
Memname = Table_Name ;
Name = Column_Name
if Data_Type in ('CHAR','VARCHAR') then do;
type = 'char';
length = cats('$',length_num);
end;
if Date_Type='DATE' then do;
type='num';
length='8';
length_num=8;
format='DATE9.';
informat='DATE9.';
end;
else do;
type='num';
format = cats('F',length_num,'.');
length='8';
length_num=8;
end;
run;
Now that you have it you can use it to write some SAS code.
For example to create you macro variables that are named after the tables.
filename code temp;
data _null_;
set sas_metadata ;
by memname varnum ;
file code ;
if first.memname then put '%macro ' memname ';' ;
put 'attrib ' name length= @;
if not missing(format) then put format= @;
if not missing(informat) then put informat= @;
put ';' ;
if last.memname then put '%mend ' memname ';' ;
run;
You could then %INCLUDE the generated code file to define the macros.
I agree with Tom. Macros are a code generation utility. I personally am not a huge macro fan but have to develop and use them a lot.
Code generation is a mindset, Do code generation in whatever way suits but I use PUT statements and then %include the code back in. Much more flexibility.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.