DATA Step, Macro, Functions and more

Removing leading and trailing blanks of all the variables in all the datasets in a library

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Removing leading and trailing blanks of all the variables in all the datasets in a library

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


Accepted Solutions
Solution
‎09-07-2017 05:01 PM
Super User
Posts: 6,534

Re: Removing leading and trailing blanks of all the variables in all the datasets in a library

[ Edited ]
Posted in reply to 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.

View solution in original post


All Replies
Solution
‎09-07-2017 05:01 PM
Super User
Posts: 6,534

Re: Removing leading and trailing blanks of all the variables in all the datasets in a library

[ Edited ]
Posted in reply to 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.

Occasional Contributor
Posts: 18

Re: Removing leading and trailing blanks of all the variables in all the datasets in a library

Posted in reply to Astounding

Thanks for the quick reply. It works.

 

Thanks

Chandan Mishra

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 149 views
  • 1 like
  • 2 in conversation