From: Subject: Date: Fri, 16 Mar 2012 12:12:42 +1100 MIME-Version: 1.0 Content-Type: text/html; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Content-Location: http://members.iinet.net.au/~brucebra/code/utility/HILDARenameStripFirst.sas X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109
%macro HILDARenameStripFirst(dataset,underscore);
/* Generates a rename statement of the form
   Lvar1=3Dvar1 Lvar2=3Dvar2 etc
   where LvarN includes all variables (except those whose name starts =
with X).
   If second parameter is text string 'underscore' then _ is added to =
start of new variable names.
   Usage example when setting up a long file:
   data long2;
     set hilda.rperson_a =
(rename=3D(%HILDARenameStripFirst(hilda.rperson_a,underscore)) in=3Din1)
         hilda.rperson_b =
(rename=3D(%HILDARenameStripFirst(hilda.rperson_b,underscore)) =
in=3Din2);
     if in1 then Wave=3D1;
     else if in2 then Wave=3D2;
     keep xwaveid xhhrhid wave _hgage;
   run;
   proc sort data=3Dlong2;by xwaveid wave;run;
   V1  BB. Aug07. I used SAS paper 107-28 by Derek Morgan as =
inspiration.
   V2  BB. Jan12. Xvariable treatment added (previously was only =
xwaveid). Underscore option added.
-------------------------------------------------------------------- */
%let DS =3D %sysfunc(open(&dataset,i)); /* open dataset to get =
variable names */
%if (&DS =3D 0) %then %put %sysfunc(sysmsg()); /* if cant open */
%else=20
  %do i=3D1 %to %sysfunc(attrn(&DS,NVARS));
    %let varname =3D %sysfunc(varname(&DS,&i)); /* get the ith =
variable name */
    %let newvarname =3D %substr(&varname,2);    /* strip off the =
first character */
    %if &underscore=3Dunderscore %then %let newvarname =3D =
_&newvarname;
    %if %upcase(%substr(&varname,1,1))^=3DX %then %do;
      %* output code here;
      &varname=3D&newvarname
    %end;
  %end;
%let CloseCode =3D %sysfunc(close(&DS));
%mend HILDARenameStripFirst;