Hello
Suppose I have a library with 10 datasets and each dataset has 10 variables. If I want to remove the leading and trailing spaces for all the variables in all the datasets, I can do it manually by using STRIP function but for that I would need to write 100 lines of code. How can I do that using macros and do it easily?
Thanks
Chandan Mishra
A few things to point out ...
First, you can only strip blanks from character variables. Numeric variables are stored in a totally different form, and do not have leading or trailing blanks.
Second, the way to process many variables in similar fashion is to use arrays:
data want;
set have;
array chars {*} _character_;
do _n_ = 1 to dim(chars);
chars{_n_} = strip(chars{_n_});
end;
run;
You don't need to know the names of the variables, and it doesn't matter how many there are ... as long as your data set contains at least one character variable.
Finally, once you do this you are likely to be unhappy with the result. Stripping blanks does not change the length of the variable. So if a variable has a length of $10 with 3 leading blanks and 3 trailing blanks, you can code:
var = strip(var);
The STRIP function really works. It removes the leading and trailing blanks. But the result gets stored in VAR, which is still defined as 10 characters long. So you will end up with 6 trailing blanks anyway.
A few things to point out ...
First, you can only strip blanks from character variables. Numeric variables are stored in a totally different form, and do not have leading or trailing blanks.
Second, the way to process many variables in similar fashion is to use arrays:
data want;
set have;
array chars {*} _character_;
do _n_ = 1 to dim(chars);
chars{_n_} = strip(chars{_n_});
end;
run;
You don't need to know the names of the variables, and it doesn't matter how many there are ... as long as your data set contains at least one character variable.
Finally, once you do this you are likely to be unhappy with the result. Stripping blanks does not change the length of the variable. So if a variable has a length of $10 with 3 leading blanks and 3 trailing blanks, you can code:
var = strip(var);
The STRIP function really works. It removes the leading and trailing blanks. But the result gets stored in VAR, which is still defined as 10 characters long. So you will end up with 6 trailing blanks anyway.
Thanks for the quick reply. It works.
Thanks
Chandan Mishra
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.
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.