DATA Step, Macro, Functions and more

PUT FUNCTION

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

PUT FUNCTION

 

 

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;


Accepted Solutions
Solution
‎04-26-2017 12:09 PM
Super User
Posts: 10,500

Re: PUT FUNCTION

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


All Replies
Trusted Advisor
Posts: 1,615

Re: PUT FUNCTION

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.

Super Contributor
Posts: 1,040

Re: PUT FUNCTION

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;

Super User
Super User
Posts: 7,401

Re: PUT FUNCTION

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.

Super Contributor
Posts: 1,040

Re: PUT FUNCTION

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;

Super User
Super User
Posts: 7,401

Re: PUT FUNCTION

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..

Solution
‎04-26-2017 12:09 PM
Super User
Posts: 10,500

Re: PUT FUNCTION

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.

Super User
Posts: 5,083

Re: PUT FUNCTION

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;

Super User
Super User
Posts: 6,500

Re: PUT FUNCTION

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);
☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 179 views
  • 2 likes
  • 6 in conversation