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

 

 

Hello,

 

How can i remove leading or trailing blanks from variable VAR and at the same time convert to a charecter variable VAR1

 

 

data want;

set have;

var1=PUT(strip(var),best.);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

STRIP is going to tell SAS that you want a character result as STRIP is a character function.

To convert character to numeric use INPUT.

VAR1=input(var,best.);

 

If that doesn't do what you want then you'll have to post example data that you have and what you actually want.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Give us a clear example of a value of var that you want to convert to var1.

 

The question doesn't make sense because if you want to convert to a character value, then var must be numeric and numerics don't have trailing or leading blanks.

--
Paige Miller
robertrao
Quartz | Level 8

THE VAR value is something like 0123456, 3456789 etc etc

This is giving me results .. Is this correct usage?

 

data want;

set have;

VAR1=STRIP(put(VAR,best.));

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This: 0123456

Is not a number.  Hence I would assume from the that var is character, in which case you need to use string parsing functions or convert to numeric like:

var=strip(put(input(var,best.),best.));

Which highlights again why posting test data in the form of a datastep is so vital to acurately answering a question.

robertrao
Quartz | Level 8

Thanks RW9.

Learnt two things here.

 

If my VAR is a number then this will suffice to convert to a charecter variable..

data want;

set have;

VAR1=STRIP(put(VAR,best.));

run;

 

If my VAR is a charecter then first convert to a numeric using INPUT?..

Then again convert to a charecter variable using PUT function and then only can I use STRIP to remove any spaces?

 

var=strip(put(input(var,best.),best.));

 

 CANT I USE JUST THE BELOW TO CONVERT STRING TO NUMERIC:

data want;

set have;

VAR1=STRIP(input(var,best.));

run;

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am quite confused with what you are asking, post some examples in a datastep of what you mean as:

If you have a character variable with spaces:
var=strip(var);

Will remove all spaces.

 

If you have a numeric variable and you want a character variable, then you need to put it first into a new variable:

newvar=strip(put(var,best.));

The strip() is needed as it will pad out the number to fit the best format.

 

Numeric variables do not have "spaces" in them, so no problem..

ballardw
Super User

STRIP is going to tell SAS that you want a character result as STRIP is a character function.

To convert character to numeric use INPUT.

VAR1=input(var,best.);

 

If that doesn't do what you want then you'll have to post example data that you have and what you actually want.

Astounding
PROC Star

The techniques are different, depending on whether VAR is character or numeric.  You will need to know that, to apply the proper code.  If you don't know, PROC CONTENTS will tell you.

 

For a character variable:

 

VAR1 = left(VAR);

 

There will still might be trailing blanks, but that is not avoidable.  If VAR has a length of 10, there is no way to change that with a function.  Even if you strip out leading and trailing blanks, SAS adds the trailing blanks back when it stores the variable using a length of 10.

 

For converting a numeric variable:

 

VAR1 = left(put(var, best.));

 

If you know something about the values to be found in your numeric variable, it might be better to assign a length to VAR1 before applying the PUT function:

 

length VAR1 $ 10;

Tom
Super User Tom
Super User

You can just add the -L modifier onto the format to remove the leading spaces.

If you know your numbers are intergers then no need to use the BEST format.  You should include a width in your format.

 

char = put(intval,F17.-L);

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 16532 views
  • 3 likes
  • 6 in conversation