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

DATA A;
X=1230;
Y=PUT(X,Z6.);
Y1=INT(LOG10(X)+1);
Y2=SUBSTR(LEFT(10**(6-Y1)),2);

Y3=CATS(Y2,X);
RUN;

 

output is:x-1230

y-001230

y1-4

y2-00

y3-001230

 

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 .

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

The key statement is here

Y1=INT(LOG10(X)+1);

 

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

For example :

x=12300;    

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

View solution in original post

3 REPLIES 3
Ksharp
Super User

The key statement is here

Y1=INT(LOG10(X)+1);

 

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

For example :

x=12300;    

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

FreelanceReinh
Jade | Level 19

@Ksharp wrote:

For example :

x=1230;    

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


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

FreelanceReinh
Jade | Level 19

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 1439 views
  • 0 likes
  • 3 in conversation