Monday, October 1, 2012

SAS and VBA (5) : replace values quickly

SAS and VBA both have their unique and quick ways to replace values in one or multiple columns.

VBA
VBA has a wonderful function Replace for several columns or regions, where the changes are likely to be happened.
Sub Replace()
With Columns("B")
.Replace "F", "Female"
.Replace "M", "Male"
End With
End Sub

SAS
User-defined format by PROC FORMAT is the best way for quick replacements.


proc format;
value $sex
'F' = 'Female'
'M' = 'Male'
;
run;

data want;
set sashelp.class;
format sex $sex.;
run;

Conclusion
For some data management operations such as string/number replacement, it is better way to use the languages' built-in features, instead of the loops and condition statements.

No comments:

Post a Comment