Hi
I got lately sick of typing especially in SQL select clauses with same named variables from different tables (like match keys).
I therefore created a macro which does all this typing for me.
Hope it will be of some use for you.
options nomprint nomlogic nosymbolgen;
%macro AttrList(LibAndTable,KeepList=,DropList=);
%global SAS_DSAttrList SAS_SQLColumnList;
%local KeepListComma DropListComma;
%let LibAndTable=%upcase(&LibAndTable);
%let KeepList=%cmpres(%upcase(&KeepList));
%let DropList=%cmpres(%upcase(&DropList));
%let KeepListComma=%sysfunc(prxchange(%str(s/\b +\b/','/oi), -1, &KeepList));
%if %bquote(&KeepListComma) ne %bquote() %then %let KeepListComma=%str(%')&KeepListComma%str(%');
%let DropListComma=%sysfunc(prxchange(%str(s/\b +\b/','/oi), -1, &DropList));
%if %bquote(&DropListComma) ne %bquote() %then %let DropListComma=%str(%')&DropListComma%str(%');
%put DropListComma= &DropListComma;
proc datasets lib=work nolist nowarn memtype=(data view);
delete __VarAndAttrListTBL;
quit;
proc sql noprint;
/* create work table __VarAndAttrListTBL containing variable names and attributes */
create table __VarAndAttrListTBL as
select
varnum
,name
,type
,length
,format
,informat
,label
from dictionary.columns
where libname="%scan(WORK.&LibAndTable,-2,'.')"
and memname="%scan(&LibAndTable,-1,'.')"
%if %bquote(&KeepList) ne %bquote() %then
%do;
and upcase(name) in (%unquote(&KeepListComma))
%end;
%if %bquote(&DropList) ne %bquote() %then
%do;
and upcase(name) not in (%unquote(&DropListComma))
%end;
/* order by varnum*/
order by name
;
/* create macro var &SAS_DSAttrList containing list of vars like needed for Data step Attrib statement */
select
catx(' ',
name
,case upcase(type)
when 'NUM' then cats('length=',length)
else cats('length=$',length)
end
,case
when format ne '' then cats('format=',format)
else ''
end
,case
when informat ne '' then cats('informat=',informat)
else ''
end
,case
when label ne '' then cats("label='",label,"'")
else ''
end
)
into :SAS_DSAttrList separated by ' '
from __VarAndAttrListTBL
/* order by varnum*/
order by name
;
/* create macro var &SAS_SQLSelectList containing list of vars like needed for SAS SQL Select clause */
select
catx(' ',
name
,cats('length=',length)
,case
when format ne '' then cats('format=',format)
else ''
end
,case
when informat ne '' then cats('informat=',informat)
else ''
end
,case
when label ne '' then cats("label='",label,"'")
else ''
end
)
into :SAS_SQLColumnList separated by ' ,'
from __VarAndAttrListTBL
/* order by varnum*/
order by name
;
quit;
%put SAS_DSAttrList = &SAS_DSAttrList;
%put SAS_SQLColumnList = &SAS_SQLColumnList;
%mend;
data have;
a2=1;
a1=1;
run;
%AttrList(work.have)
data want;
attrib &SAS_DSAttrList;
set have;
run;
proc contents data=want;
quit;
Cheers
Patrick
Had to change 'order by varnum' to 'order by name' to cover for this requirement
Message was edited by: Patrick