Automatic macro code generation

Accepted Solution Solved
Reply
Super Contributor
Posts: 704
Accepted Solution

Automatic macro code generation

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;

 


Accepted Solutions
Solution
‎02-22-2018 04:24 PM
Super User
Super User
Posts: 7,932

Re: Automatic macro code generation

[ Edited ]

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.

View solution in original post


All Replies
Super User
Posts: 23,235

Re: Automatic macro code generation

Lexjansen has some demos on this.

Solution
‎02-22-2018 04:24 PM
Super User
Super User
Posts: 7,932

Re: Automatic macro code generation

[ Edited ]

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.

Regular Contributor
Posts: 150

Re: Automatic macro code generation

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 119 views
  • 1 like
  • 4 in conversation