## Help to understand numeric-character-numeric conversion?

Solved
Super Contributor
Posts: 338

# Help to understand numeric-character-numeric conversion?

Hi SAS Forum,

DATA want;

SET have;

IF PUT(BRANCH,\$2.) NE ' ' THEN

BRANCH = INPUT(PUT(BRANCH,\$2.),2.);

RUN;

I take the first part of the above

IF PUT(BRANCH,\$2.) NE ' '

The meaning of this is:

“ A numeric variable named “Branch” is converted into a character variable with a length of \$2. And if the value of the converted i"Branch" variable is not equal to missing”

Question 1.

Is my interpretation correct?

Question 2.

But \$2. should be the initial state and not eventual state. How come the initial state of a numeric variable is \$2.

Question 3.

What is the meaning of next segment which is:

Branch  = INPUT(PUT(Branch,\$2.),2.);

Thanks

Mirisage

Accepted Solutions
Solution
‎03-11-2013 11:07 PM
Super User
Posts: 8,127

## Re: Help to understand numeric-character-numeric conversion?

Code does not make sense.  There is no way to tell from that code fragment whether the original variable BRANCH read from the dataset HAVE is numeric or character. (or even which one the programmer thought it was supposed to be).

If Branch is numeric then you should not attempt to use a character format such as \$2. with it.  SAS will give a warning that variable BRANCH is already defined as numeric.  It will then automatically adjust and use the numeric format 2. in the PUT() function calls. Even if you fix that then testing whether the character string generated by the PUT() function is blank does not make much sense.  Why not just test if the value of BRANCH is missing?

If Branch is character then you should not attempt to assign it the numeric value that the INPUT() function will generate when using the numeric informat 2. .  In that case if you want to truncate the string that is stored in BRANCH then just use SUBSTR() function.

Question 1).  No.  To convert a numeric variable into a character string you could use the PUT() function, but you would use it with a numeric format.

Question 2)  Question does make any sense. There is no initial state here, the PUT() and INPUT() functions are just doing what the programmer tells them to do.

Question 3) There is no meaning. Because of the use of the PUT() function on a numeric variable with a character format it makes no sense.

Did it come with any comments that explain what they were trying to do?  Can you tell from the rest of the original program?

Perhaps they want to read the two most significant digits of a number where the magnitude of the number varies widely?  If want the first two digits then why not use:  substr(left(put(branch,32.)),1,2)

All Replies
Solution
‎03-11-2013 11:07 PM
Super User
Posts: 8,127

## Re: Help to understand numeric-character-numeric conversion?

Code does not make sense.  There is no way to tell from that code fragment whether the original variable BRANCH read from the dataset HAVE is numeric or character. (or even which one the programmer thought it was supposed to be).

If Branch is numeric then you should not attempt to use a character format such as \$2. with it.  SAS will give a warning that variable BRANCH is already defined as numeric.  It will then automatically adjust and use the numeric format 2. in the PUT() function calls. Even if you fix that then testing whether the character string generated by the PUT() function is blank does not make much sense.  Why not just test if the value of BRANCH is missing?

If Branch is character then you should not attempt to assign it the numeric value that the INPUT() function will generate when using the numeric informat 2. .  In that case if you want to truncate the string that is stored in BRANCH then just use SUBSTR() function.

Question 1).  No.  To convert a numeric variable into a character string you could use the PUT() function, but you would use it with a numeric format.

Question 2)  Question does make any sense. There is no initial state here, the PUT() and INPUT() functions are just doing what the programmer tells them to do.

Question 3) There is no meaning. Because of the use of the PUT() function on a numeric variable with a character format it makes no sense.

Did it come with any comments that explain what they were trying to do?  Can you tell from the rest of the original program?

Perhaps they want to read the two most significant digits of a number where the magnitude of the number varies widely?  If want the first two digits then why not use:  substr(left(put(branch,32.)),1,2)

Contributor
Posts: 29

## Re: Help to understand numeric-character-numeric conversion?

Hi,

branch= input(put(branch,\$2.),2.)

you are

1) converting numeric variable branch with format \$2. to character and then

2) again you are converting this character to numeric variable which results in the same values as in source dataset.

in stead of writing that whole code , you can simply write as:

if branch ne . then branch1=branch;

Hope it helps.

Super Contributor
Posts: 338

## Re: Help to understand numeric-character-numeric conversion?

Hi Tom and Mano,

Many thanks to both of you.

Regards

Mirisage

🔒 This topic is solved and locked.