/********************************************************************************************** Program: delnpsc.sas Purpose: General purpose macro to delete or replace the non printable/special characters in the specified dataset. Input: Dataset that need NPSC clean up. Output: The same input dataset will be output in which the NPSC will be deleted or replaced. If no NPSC are were present in the input dataset, then the input and output datasets will be exactly same. Macro parameter description: ds= Dataset in which the non printable/special characters need to be deleted or replaced. This is a required parameter. If not provided, then a warning note will be issued in the log. cvarlist= List of character variables separated by space, that may contains the non printable/special characters and need to be deleted or replaced. If nothing is specified for the cvarlist, then all the character variables in the specified dataset will be used as the character variable list. replchar= Character used to replace the non printable/special characters. If none specified, then the non printable/special characters will be completely deleted. replchar is not enclosed in quotations marks, but it should be specified / enclosed in the parenthesis of the %str(). Usage: delnpsc macro is called outside the data step as follows: %delnpsc (ds=ae, cvarlist=, replchar=%str()); *Deleting the NPSC; %delnpsc (ds=cm, cvarlist=aeterm, replchar=%str( )); *Replacing NPSC with ‘SPACE’; %delnpsc (ds=ex, cvarlist=var1 var2, replchar=%str(?)); *Replacing NPSC with ‘?’; **********************************************************************************************/ %macro delnpsc (ds=, cvarlist=, replchar=%str()); %local nullstr warn1 warn2; %let warn1=WAR; %let warn2=NING; %if "&ds"="&nullstr" %then %do; %put &warn1&warn2 (User defined): No dataset has been specified in the delnpsc macro call.; %end; %else %if not %sysfunc(exist(&ds)) %then %do; %put &warn1&warn2 (User defined): The specified dataset in the macro call does not exist; %put Please specify a valid dataset name.; %end; %else %do; %if "&cvarlist"="&nullstr" %then %let cvarlist=_character_; data &ds (drop=i npschars %if "&replchar"^="&nullstr" %then position;); set &ds; array charvars &cvarlist; length npschars $161; *161 is the total number of characters from 0 to 31, 127 to 255; retain npschars; if _N_=1 then do; do i=0 to 31, 127 to 255; if i=0 then npschars=byte(i); else npschars=trim(npschars)||byte(i); end; end; do over charvars; %if "&replchar"="&nullstr" %then %do; if indexc(charvars, npschars) then put "USERWAR" "NING: Special char found in data=&ds " charvars=; charvars=compress(charvars, npschars); %end; %else %do; do until (position=0); position=indexc(charvars, npschars); if position>0 then substr(charvars, position, 1)="&replchar"; end; %end; end; run; %end; %mend; /**********************/ /*** End of program ***/ /**********************/