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

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
  • 10624 views
  • 3 likes
  • 2 in conversation