0% found this document useful (0 votes)
96 views6 pages

SAS Macro To Fill Missing Values in A Dataset With Specified Values For Numeric and Character Values

This document describes a SAS macro called %fill that is used to replace missing values in datasets with default values. The %fill macro takes an input dataset, output dataset, lists of numeric and character variables, and replacement values. It loops through the numeric and character variables, replacing missing values with the fill values provided. Examples are given showing how to call the macro to replace all missing values or just specific variables.

Uploaded by

Narendra Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views6 pages

SAS Macro To Fill Missing Values in A Dataset With Specified Values For Numeric and Character Values

This document describes a SAS macro called %fill that is used to replace missing values in datasets with default values. The %fill macro takes an input dataset, output dataset, lists of numeric and character variables, and replacement values. It loops through the numeric and character variables, replacing missing values with the fill values provided. Examples are given showing how to call the macro to replace all missing values or just specific variables.

Uploaded by

Narendra Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Often a times, we get a dataset with missing values for certain variablesAnd for reporting purposes we need to convert

them some or all of those variable values to default values (for e.g 0 for numeric values or NULL for character values).. Try using this macro for such type of purposes... For eg. data test; input fruit $ shop $ count2 count3 count4 ; datalines; apple wal . 2 3 coconut . 30 3 . . sams 40 . . eggs . 50 . ...45 ; run; Input dataset with couple of missing values for numeric(count1, 2,3,4 ) and character (fruit, shop) variables.

Note: Code for %splitmacroval(numvars,num,numcnt,dlm=' ') is at http://sastechies.blogspot.com/2009/11/sas-macro-to-split-macro-variables.html /** Use the macro like this... inpdsn- name of the input dataset outdsn- name of the output dataset numvars= - give the list of numeric variables to be filled in with charvars= - give the list of character variables to be filled in with fillnums= the numeric value to use to fill the dataset fillchars=the character value to use to fill the dataset

%fill(test,out,fillnums=0,fillchars=A); **/ %macro fill(inpdsn,outdsn,numvars=,charvars=,fillnums=,fillchars=); data &outdsn; /*output dataset name*/ set &inpdsn; %if &numvars eq ALL %then %do; array nums(*) _numeric_ ; /*assign all numeric variables to the array nums */ %end; %else %do; %splitmacroval(numvars,num,numcnt,dlm=' ') array nums(&numcnt.) %do k=1 %to &numcnt; &&num&k %end;; /*assign all numeric variables to the array nums */ %end; %if &charvars eq ALL %then %do; array chars(*) _character_ ; /*assign all character variables to the array chars */ %end; %else %do;

%splitmacroval(charvars,chr,chrcnt,dlm=' ') array chars(&chrcnt.) %do k=1 %to &chrcnt; &&chr&k %end;; /*assign all character variables to the array chars */ %end; /* for every numeric variable assign 0 to the variable if the value is missing */ do i=1 to dim(nums); if nums(i)=. then nums(i)=&fillnums; end; /* for every Character variable assign A for eg. to the variable if the value is missing */ do i=1 to dim(chars); if chars(i)='' then chars(i)="&fillchars"; end; run; %mend fill;

Output1 after applying the macro %fill(test,out1,numvars=ALL,charvars=ALL,fillnums=0,fillchars=A);

Output2 after applying the macro %fill(test,out2,numvars=count3 count4,charvars=shop ,fillnums=0,fillchars=A);

Read more @ http://sastechies.blogspot.com/2009/11/sas-macro-to-fill-missing-values-in_25.html

You might also like