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;
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.
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.
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;
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.
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;
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..
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.
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;
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);
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!
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.
Ready to level-up your skills? Choose your own adventure.