BookmarkSubscribeRSS Feed
owenwqp1
Obsidian | Level 7

Hi,

I recently read a paper "A Macro to Modify Attributes for All Character Variables in SAS". But  it seems that the macro provided could only deal with blanks. what I really want to do is to compress characters like"-,.,,@,$" in observations.

The macro is posted below. Anyone could tell me how to revise the macro so that I can  compress those characters? Thanks!

*****PART1;

%macro Modify_All_Character_Variables;
%let ExitVariable=1;
/*checking whether or not required input parameters are defined*/
%if %length(&datasets)>0 and %upcase(&datasets)^= and %length(&function_name)>0 and
%upcase(&function_name)^=
%then %let ExitVariable=0;
%if %eval(&ExitVariable)>0 %then %do;
%put;%put !!! Not enough parameters defined to run this macro !!!;%put;
%goto LastExit;
%end;
/*checking whether or not input "function_name" parameter is valid*/
%if %length(%sysfunc(&function_name(TEST)))<4 %then %do;
%put; %put !!! Please provide valid SAS character function name !!!;%put;
%goto LastExit;
%end;


/*This portion of the code checks whether or not the user has provided both of the required inputs by using the
%SYSFUNC and %LENGTH functions. The “ExitVariable” macro variable acts as an ON/OFF switch with a default
value of '1' (OFF condition). Only when both input parameters are defined does its value get reset to '0' and the
macro proceeds further. If any of the two parameters are not defined, an error message will be displayed in the log
window. The second check validates the applicability of the user-defined SAS function by testing the changes to the
length of the word “TEST”. Failure of either check diverts the code to go to the “LastExit” statement at the end of the
macro.*/


*****PART2;

options nonotes;
/*counting the number of submitted datasets*/
data _NULL_; call symput('DSNo',count("&datasets",' ')+1); run;
%do D=1 %to &DSNo;
%let sel_dataset=%scan(&datasets,&D,' ');
%put;
%put Applying "&function_name" function to the dataset: &sel_dataset;
%if %sysfunc(exist(&sel_dataset))=0 %then %do;
%put !!! "&sel_dataset" does not exist. Please check. !!!; %put;
%goto Exit;
%end;


/*This portion of code counts the number of “space” characters in the user-defined “datasets” parameter. The best
method to count “useful” spaces is to use the CALL SYMPUT function inside a DATA _NULL_ step. Then a %DO
loop is set with the limit of as many datasets as needed to process. Within the %DO loop, “sel_dataset” names are
assigned sequentially, and the presence of the respective SAS table is also checked using and EXIST functions.*/


********PART3;

/*identifying the character variables from the SAS data table*/
proc contents data=&sel_dataset out=Tmp noprint; run;
proc sql noprint;
%let sel_Cvariables=; %let sel_Nvariables=;
select compress(left(NAME)),
catt(compress(left(NAME)),"=&function_name(",compress(left(NAME)),')')
into:sel_Cvariables separated by ', ',:sel_Nvariables separated by ', ' from Tmp where
Type=2;

drop table Tmp;
%if %length(&sel_Nvariables)=0 %then %do;
%put !!! There are no character variables in this dataset !!!;%put;
%goto Exit;
%end;


/*Using PROC CONTENTS, information about character variables within each of the input data tables is generated. If
there are no character variables in the given dataset, the code is diverted to the “Exit” statement before the loop
%END statement. In addition, a message is written to the log window regarding this issue. Otherwise, SQL syntax for
updating character variables with the user-defined SAS function gets generated. This information is stored in SAS
memory with the help of the “sel_Nvariable” macro variable.*/


**********PART4;


/*applying the character function specified by the user*/
update &sel_dataset set &sel_Nvariables;
quit;
%put "&function_name" converted variables: &sel_Cvariables;
%Exit : ;
%let sel_dataset=; %let Sel_cvariables=; %let Sel_nvariables=;
%put;
%end;
%put;%put !!! Done. Thanks for using "Modify_All_Character_Variables" macro !!!;%put;
%LastExit: ;
%symdel datasets function_name;
option notes;
%mend ;

2 REPLIES 2
Reeza
Super User

You don't need a macro IMO

data want;

set have;

array all(*) _character_;

do i=1 to dim(all);

all(i)=compress(all(i), <*insert proper functions here*>);

end;

run;

Peter_C
Rhodochrosite | Level 12

n wrote:

Hi,

I recently read a paper "A Macro to Modify Attributes for All Character Variables in SAS". But  it seems that the macro provided could only deal with blanks. what I really want to do is to compress characters like"-,.,,@,$" in observations.

The macro is posted below. Anyone could tell me how to revise the macro so that I can  compress those characters? Thanks!

select compress(left(NAME)),
catt(compress(left(NAME)),"=&function_name(",compress(left(NAME)),')'

Given that

Result = compress( source, 'abcdefgijklmnopqrstuvwxyz', 'ki' );

keeps only upper and lowercase letters in the source variable without fully understanding the macro, I assume you want to have some impact on the functionality of the macro that I do not understand.

I would recommend you contact the author of the paper/macro to see if the functionality you seek can be achieved.

Who was the author?

peterC

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 789 views
  • 0 likes
  • 3 in conversation