Date: Fri, 8 Oct 1999 17:22:15 -0700 "CUMMING, GORDON (PB)" "SAS(r) Discussion" To: "PELIC, DANIEL" "CUMMING, GORDON (PB)" Re: Convert from decimal to Base36 To: SAS-L@LISTSERV.VT.EDU text/plain; charset="iso-8859-1"

You can try this code I wrote 25 years ago and converted just now to SAS that works still with any base from 2 to 36 with decimals to four positions rounded.

%LET SIG = 4; %LET BASE = 36; %LET DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

DATA _NULL_;

LENGTH RESULT \$ 20; INPUT @1 VALUE;

RESULT = ''; X = ABS(VALUE);

DP = MAX(INT(LOG(X)/LOG(&BASE)),0); DO I = DP TO 0 BY -1; DIGIT = INT(X / &BASE ** I); X = X - DIGIT * &BASE ** I; RESULT = TRIM(RESULT) || SUBSTR(&DIGITS, DIGIT+1, 1); END;

IF X > 0 THEN DO; RESULT = TRIM(RESULT) || '.'; DO I=-1 BY -1 UNTIL (X=0 OR I=(-&SIG-1)); DIGIT = INT(X / &BASE ** I); X = X - DIGIT * &BASE ** I; RESULT = TRIM(RESULT) || SUBSTR(&DIGITS,DIGIT+1,1); END;

IF I=(-&SIG-1) THEN DO; RESULT = SUBSTR(RESULT,1,LENGTH(TRIM(RESULT))-1); DIGIT = INDEX(&DIGITS,SUBSTR(TRIM(RESULT), LENGTH(TRIM(RESULT)),1))-1; DO WHILE (DIGIT = 0); RESULT = SUBSTR(RESULT,1,LENGTH(TRIM(RESULT))-1); DIGIT = INDEX(&DIGITS,SUBSTR(TRIM(RESULT), LENGTH(TRIM(RESULT)),1))-1; END; END;

result = compress(result); IF I=(-&SIG-1) AND DIGIT > &BASE / 2 THEN DO; LAST = 0; DO UNTIL (LAST); DIGIT = INDEX(&DIGITS,SUBSTR(TRIM(RESULT), LENGTH(TRIM(RESULT)),1))-1; DIGIT = MOD(DIGIT+1,&BASE); RESULT = SUBSTR(RESULT,1,LENGTH(TRIM(RESULT))-1); IF DIGIT > 0 THEN DO; LAST = 1; RESULT = TRIM(RESULT)||SUBSTR(&DIGITS,DIGIT+1,1); END; IF SUBSTR(TRIM(RESULT),LENGTH(TRIM(RESULT)),1) = '.' THEN DO; RESULT = SUBSTR(RESULT,1,LENGTH(TRIM(RESULT))-1); C = 1; DO I=LENGTH(TRIM(RESULT)) TO 1 BY -1; DIGIT = INDEX(&DIGITS,SUBSTR(RESULT,I,1))-1; DIGIT = MOD(DIGIT+C,&BASE); SUBSTR(RESULT,I,1) = SUBSTR(&DIGITS,DIGIT+1,1 IF DIGIT THEN C = 0; END; IF C THEN RESULT = '1'||COMPRESS(RESULT); LAST = 1; END; END; END; END;

IF VALUE < 0 THEN RESULT = '-'||COMPRESS(RESULT);

PUT @5 VALUE= @25 RESULT=;

CARDS; 3968 75 10 -37.40362 0.000643 16.277778 46655.9999996 ; RUN;

VALUE=3968 RESULT=328 VALUE=75 RESULT=23 VALUE=10 RESULT=A VALUE=-37.40362 RESULT=-11.EJ3A VALUE=0.000643 RESULT=0.00U VALUE=16.277778 RESULT=G.A VALUE=46655.999999 RESULT=1000 NOTE: The DATA statement used 0.04 CPU seconds and 8851K.

> -----Original Message----- > From: PELIC, DANIEL [mailto:daniel.pelic@BELL.CA] > Sent: Friday, October 08, 1999 10:43 AM > To: SAS-L@VM.MARIST.EDU > Subject: Convert from decimal to Base36 > > > Is there a Format/Informat/Function that > could convert a decimal (Base10) expression > to a Base36 expression ? > > Please note that I am not asking Base32 but > Base36. > > I know that there is already a Binary, Octal > and Hex Format in SAS. Is there a similar > Format for Base36 ? > > Thank you in advance. >

Back to: Top of message | Previous page | Main SAS-L page