Renaming Variables with PROC Datasets

(Apologies in advance for not indenting.  I need to noodle with css or something).

Say you import an xlsx file…

libname logit '\\psf\Home\Desktop\';
proc import datafile="\\psf\Home\Desktop\bc.xlsx" out=logit.bc dbms=xlsx replace;
run;

…and it has no header row.  SAS will come up with its own variable names and labels:

ods pdf file='\\psf\Home\Desktop\01.pdf' style=SASdocprinter;
options nodate;
title Proc Contents One-After Proc Import;
proc contents data=logit.bc;
run;
title;
ods pdf close;

Ick.

before

Use PROC Datasets to rename and relabel variables:

/* Renaming and labeling variables */

proc datasets library=logit;
modify bc;
rename _24=wife_age  _2=wife_edu  _3=hus_edu  _3_1=num_child  _1=wife_rel 
_1_1=wife_work   _2_1=hus_occ  _3_2=sol  _0=media  _1_2=bc;
label wife_age="Wife's Age"  wife_edu="Wife's Education"  hus_edu="Husband's Education"num_child='Number of Children'  wife_rel="Wife's Religion" 
wife_work="Wife Work Outside Home?"  hus_occ="Husband's Occupation"
sol='Standard of Living'  media='Media Exposure'  bc='Contraceptive Method';
run;
quit;
ods pdf file='\\psf\Home\Desktop\02.pdf' style=SASdocprinter;
options nodate;
title Proc Contents Two-After Proc Datasets;
proc contents data=logit.bc;
run;
title;
ods pdf close;

That’s better!

after

It’s best not to use a data step simply to change the name of a SAS dataset or columns in a SAS dataset. This requires the entire dataset to be rewritten. Insead use proc datasets, which changes the names without needing to read the observations in the dataset.

Thanks!


 

Original dataset from “Contraceptive Method Choice” courtesy of the UCI Machine Learning Repository.