BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chandan_mishra
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

chandan_mishra
Obsidian | Level 7

Thanks for the quick reply. It works.

 

Thanks

Chandan Mishra

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 11853 views
  • 3 likes
  • 2 in conversation