<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Automatic macro code generation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436893#M282200</link>
    <description>&lt;P&gt;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)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Feb 2018 21:48:37 GMT</pubDate>
    <dc:creator>SASPhile</dc:creator>
    <dc:date>2018-02-13T21:48:37Z</dc:date>
    <item>
      <title>Automatic macro code generation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436893#M282200</link>
      <description>&lt;P&gt;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)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2018 21:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436893#M282200</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2018-02-13T21:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic macro code generation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436920#M282201</link>
      <description>&lt;P&gt;Lexjansen has some demos on this.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2018 23:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436920#M282201</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-13T23:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic macro code generation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436981#M282202</link>
      <description>&lt;P&gt;Not sure that generating macros adds anything, but it is not hard to generate code from data.&lt;/P&gt;
&lt;P&gt;The PUT statement is a really good way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp; So if you have this existing Metadata:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could convert it to metadata that would work for SAS&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Now that you have it you can use it to write some SAS code.&lt;/P&gt;
&lt;P&gt;For example to create you macro variables that are named after the tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then %INCLUDE the generated code file to define the macros.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2018 04:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/436981#M282202</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-14T04:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic macro code generation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/437415#M282203</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 04:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatic-macro-code-generation/m-p/437415#M282203</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2018-02-15T04:25:39Z</dc:date>
    </item>
  </channel>
</rss>

