Okay, sorry if I sounded condescending earlier .. I didn't mean to! The code is difficult for someone new to SAS to understand, because it is using the concept of a SAS macro (among other things) that, honestly, are difficult concepts to grasp. The code, as written, will ONLY work if the variables names are uppercase. That, of course, could easily lead to a problem. The 3 variables in the beginning of renames should be either be removed or replaced with the variables that they supposedly represent. I actually preferred KSharp's originally suggested code and will explain a slight modification of it below: The first part is just running your original datastep that created the file called renames. There is nothing in it that requires a SAS macro, thus I just ran it as a separate datastep: data renames ; length original newname $32 ; original = 'B9_' ; newname = 'submitdate' ; output ; original = 'B9_' ; newname = 'cannotfillout' ; output ; original = 'B9_' ; newname = 'otherspecify' ; output ; original = 'B9_28' ; newname = 'dateofeval' ; output ; original = 'B9_29' ; newname = 'position' ; output ; original = 'B9_30' ; newname = 'otherspecify__1' ; output ; original = 'B9_31' ; newname = 'attending' ; output ; original = 'B9_1' ; newname = 'weight' ; output ; original = 'B9_2' ; newname = 'breastpain' ; output ; original = 'B9_3' ; newname = 'karnofsky_kps' ; output ; original = 'B9_4_2' ; newname = 'areas_scar' ; output ; original = 'B9_4_3' ; newname = 'areas_fold1' ; output ; original = 'B9_4_4' ; newname = 'areas_fold2' ; output ; original = 'B9_4_5' ; newname = 'areas_other' ; output ; original = 'B9_4_1' ; newname = 'areas_na' ; output ; original = 'B9_4_6' ; newname = 'moistelsewhere' ; output ; original = 'B9_5_2' ; newname = 'areas2_scar' ; output ; original = 'B9_5_3' ; newname = 'areas2_fold1' ; output ; original = 'B9_5_4' ; newname = 'areas2_fold2' ; output ; original = 'B9_5_5' ; newname = 'areas2_other' ; output ; original = 'B9_5_1' ; newname = 'areas2_na' ; output ; original = 'B9_5_6' ; newname = 'dryelsewhere' ; output ; original = 'B9_6_1' ; newname = 'breasterythema' ; output ; original = 'B9_6_2' ; newname = 'locationerythema_scar' ; output ; original = 'B9_6_3' ; newname = 'locationerythema_fold1' ; output ; original = 'B9_6_4' ; newname = 'locationerythema_fold2' ; output ; original = 'B9_6_5' ; newname = 'locationerythema_other' ; output ; original = 'B9_7_1' ; newname = 'rectreatments_calendula' ; output ; original = 'B9_7_2' ; newname = 'rectreatments_aquaphor' ; output ; original = 'B9_7_3' ; newname = 'rectreatments_alra' ; output ; original = 'B9_7_4' ; newname = 'rectreatments_miaderm' ; output ; original = 'B9_7_5' ; newname = 'rectreatments_biafene' ; output ; original = 'B9_7_6' ; newname = 'rectreatments_silvadene' ; output ; original = 'B9_7_7' ; newname = 'rectreatments_aloe' ; output ; original = 'B9_7_8' ; newname = 'rectreatments_corn' ; output ; run ; /* Next, I used a datastep to create a sample dataset, like the one that was modified in the original code*/ data list2; input B9_1 B9_4_2; cards; 1 2 3 4 ; /* Then I created one macro variable, called &thefile. It is the only statement that you would have to change in order to run the code on a different file */ %let thefile=LIST2; /* Then I used proc sql to create a dataset, called applicable_renames that will contain the names of all of the variables that exist in the file referred to by &thefile. The file is both created and sorted in the proc sql call*/ proc sql noprint; create table applicable_renames as select name as original from dictionary.columns where libname='WORK' AND memname="&thefile." order by original ; quit; /* Then I sorted the file renames in order of the variable original*/ proc sort data=renames; by original; run; /* Then I merged the two files, applicable_renames and renames, so that applicable_renames would only include those variables that exist in the file referred to by &thefile. and had a recode identified in renames*/ data applicable_renames; merge applicable_renames (in=a) renames (in=b); by original; if a and b; run; /* Now, using KSharp's proposed method, proc datasets only has to rename the variables that really have to be renamed. His code works by using call execute to create a program that will run, by itself, as soon as SAS is finished running the data_null step*/ data _null_; set applicable_renames end=last; if _n_ eq 1 then call execute( "proc datasets nolist lib=work; modify &thefile.; rename"); call execute(catx('=',original,newname)); if last then call execute(';quit;'); run; /* EXTRA CREDIT: I liked KSharp's approach because I really don't think you need to rename the variables but, rather, just need to assign labels to them. If that is true, then instead of the previous data_null step, all you would have to change in the above code is to run the following data_null datastep instead:*/ data _null_; set applicable_renames end=last; if _n_ eq 1 then call execute( "proc datasets nolist lib=work; modify &thefile.; label"); call execute(catx('=',original,newname)); if last then call execute(';quit;'); run;
... View more