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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.