BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASPhile
Quartz | Level 8

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Reeza
Super User

Lexjansen has some demos on this.

Tom
Super User Tom
Super User

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.

AlanC
Barite | Level 11

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.

https://github.com/savian-net

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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