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

Hi SAS Forum,

Could you please help me to understand this code piece.

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

Your help is appreciated.

Thanks

Mirisage

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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)

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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)

mano
Calcite | Level 5

Hi,

As per your question :-

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.

Mirisage
Obsidian | Level 7

Hi Tom and Mano,

Many thanks to both of you.

Regards

Mirisage

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 3525 views
  • 3 likes
  • 3 in conversation