|
|
|
How can I make a global change to all the blank values to the actual correct value within a set of variables?
|
There are a series of SAS system views in the SASHELP library, which contain useful information. These
can be processed like any other dataset, using WHERE and IF statements to feed values back into your
programs. The following views are a few of the most useful.
| VCATALG |
Library name, Member name, Member type, Object name, Object type, Object description, Date modified, Object alias. |
| VCOLUMN |
Library name, Member name, Member type, Column name, Column type, Column length, Column position, Column number, Column label, Column format, Column informat, Column index type |
| VEXTFL |
Fileref, Path name (fully qualified file name and path), Engine name. |
| VMEMBER |
Library name, Member name, Member type, Engine name, Indexes, Path name. |
| VOPTION |
Session option name, Session option setting, Option description. |
| VSCATLG |
Library name, Member name. |
| VSLIB |
Library name, Path name |
| VSTABLE |
Library name, Member name |
| VTABLE |
Library name, Member name, Member type, Dataset label, Date created, Date modified, Number of observations, Observation length, Number of variables, Type of password protection, Compression routine, Reuse space, Bufsize, Number of deleted observations, Type of indexes. |
| VTITLE |
Title number, Title text. |
This example prints a list of the libraries currently assigned on your system.
proc print data = sashelp.vslib;
run;
The following example globally replaces all blank values within variables selected to another chosen value.
Step 1
Sometimes you are presented with data that contains blank values for character fields. However in
reality, these blank values may have a value. In the example below we create a dataset containing
details of current account balances. Zero balances are currently represented with blanks, but SAS
will treat these as missing values. We will use SAS views to build a macro that will replace all
instances of these missing values with their 'true' values. If you have many variables that need values
replacing (not just blank or character) a macro can prove to be more maintainable than tailor written
SAS code.
data balances;
input jan03 $9. feb03 $9. mar03 $9. apr03 $9. may03 $9. jun03 $9.;
cards;
OverdrawnOverdrawnOverdrawnOverdrawnOverdrawnOverdrawn
Credit Credit Credit Overdrawn
OverdrawnCredit Overdrawn OverdrawnCredit
OverdrawnOverdrawn Credit Credit
;
run;
proc print data=balances;
run;
Step 2
This is where we use a SAS view, in this instance sashelp.volumn to populate the view tmpz1_char1
(remember that views take up less space than datasets) with details of the char variable names held
within the WORK.BALANCES data set. Notice in the SQL, we use uppercase letters for the library and
dataset name. This is because the view creates uppercase values for the libname and memname fields.
proc sql;
create view tmpz1_char1 as select * from sashelp.vcolumn
where libname='WORK' and memname='BALANCES' and type='char';
quit;
Step 3
Having created a view containing the names of the character variables we can now use these to populate the values of our macro variables. We will be creating a number of macro variables with the prefix charz. For this example the number of macro variables will be 6, charz1-charz6. We
also create a macro variable count that contains a value relating the number of macro variables generated.
data _null_;
set tmpz1_char1 end=eof;
call symput('charz'||left(put(_n_,8.)),(trim(name)));
if eof then call symput('count',put(_n_,8.));
run;
Step 4
This is where we write our macro
%macro testdatz (indata,outdata,replacestr,withstr);
We use 4 positional parameters for this macro. The first 'indata', will be used to tell us which
dataset we wish to replace the values in. The second is 'outdata' which is the name given to the
resulting dataset created after the replacements have been made. The 'replacestr' parameter is the
string that you would like to replace and finally 'withstr' is the character value that replaces
existing values.
%local i; - will be used to generate a loop
data &outdata; - create resulting dataset
set &indata; - access the input dataset
%do i=1 %to &count; - use the count macro variable to define the number of loops
if &&charz&i="&replacestr" then &&charz&i="&withstr"; - here's where we replace the values within the dataset
%end;
run;
This is the end of the macro.
%mend;
Step 5
Here's where we run the macro. Notice the values of our conditional parameters
%testdatz (work.balances,work.balances1, ,Zero);
Step 6
Lets print the resulting dataset work.balances1 to see the replaced values.
proc print data=balances1;
run;
Note: The above code has been tested using SAS 8.2 on the Windows operating system.
|