DATA Step, Macro, Functions and more

Substring and left function

Occasional Contributor
Posts: 15

Substring and left function




output is:x-1230






so i wanted to know how this program is working 

in line Y2=SUBSTR(LEFT(10**(6-Y1)),2);,what is the use of "10**",how this is working and what impact it has on the numbers.

and how it got to know that it had to substract 4 from y1 without refrencing y .



Super User
Posts: 10,782

Re: Substring and left function

[ Edited ]

The key statement is here



LOG10(X) means get the power of X by 10 .

For example :


INT(LOG10(X)) will get 4 ,which is power of X ( 10^4=10000).

Trusted Advisor
Posts: 1,252

Re: Substring and left function

@Ksharp wrote:

For example :


INT(LOG(X)) will get 4 ,which is power of X ( 10^4=10000).

You mean INT(LOG10(X)+1).

Trusted Advisor
Posts: 1,252

Re: Substring and left function

[ Edited ]

Apparently, lines 4 - 6 of this data step are an attempt to replicate the result obtained in line 3, but without using PUT function and Z6. format.


The plan is:

  1. to store the number of digits in the value of X in variable Y1
  2. to create a character string consisting of as many zeros as needed to pad the value of X with leading zeros aiming at a total of 6 digits. (In the example, 10**(6−4)=10**2=100 is implicitly converted to a character string by applying character function LEFT to it. As the implicit numeric-to-character conversion uses format BEST12., the resulting temporary string consists of nine leading blanks followed by '100'. The LEFT function moves these three digits to the first three places of the 12-character string in order to get the desired '00' by means of SUBSTR(..., 2), i.e. taking all characters starting from the second.)
  3. to concatenate the string of zeros created in step 2 and the original number.

However, it must be said that this is poor programming style.

  • Given the existence of the simple solution in line 3 it is unnecessarily cumbersome and awkward and will increase run time.
  • It does not work in several cases, e.g. X<=0, X>999999 or X not an integer.
  • It does not generalize well (e.g. to 13 digits rather than 6; see BEST12. format mentioned above).
  • The implicit numeric-to-character conversion enforced by applying a character function (LEFT) to a number is frowned upon as is the accompanying note in the log ("Numeric values have been converted to character values ...").
  • The CATS function (btw enforcing another implicit numeric-to-character conversion, but without a note in the log) produces a character variable of length 200, whereas Y has the appropriate length 6.
  • Intermediate variables (Y1, Y2) need to be dropped.
Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation