As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Tips:Calculate logarithm of any base"

From sasCommunity
Jump to: navigation, search
m
(gardening)
 
Line 1: Line 1:
 
<div style="float:right;width:20%;"><source lang="sas">
 
<div style="float:right;width:20%;"><source lang="sas">
 
data show;
 
data show;
val=55;
+
    val = 55;
b2 = log2(val);
+
    b2 = log2(val);
v2 = log(val) / log(2);
+
    v2 = log(val) / log(2);
put b2= ;
+
 
put v2=;
+
    put b2= ;
 +
    put v2=;
 
run;</source></div>  
 
run;</source></div>  
[[SAS]] supports only 3 bases for the log function, namely, <big><source lang="sas" enclose="none">{2, 10, exp(1)}</source></big>, corresponding to functions <big><source lang="sas" enclose="none">log2()</source></big>, <big><source lang="sas" enclose="none">log10()</source></big>, and <big><source lang="sas" enclose="none">log()</source></big>, respectively.
+
[[SAS]] supports only 3 bases for the log [[function]], namely, <big><source lang="sas" enclose="none">{2, 10, exp(1)}</source></big>, corresponding to functions <big><source lang="sas" enclose="none">log2()</source></big>, <big><source lang="sas" enclose="none">log10()</source></big>, and <big><source lang="sas" enclose="none">log()</source></big>, respectively.
  
Sometimes, we need to calculate log value of any positive base number, just as in Excel or some other programming environment, we have function like <big><source lang="sas" enclose="none">log(Val, Base)</source></big>. It is not hard to do so by noting that we can calculate logarithms with other base values by dividing the result of the <big><source lang="sas" enclose="none">log()</source></big> function with <big><source lang="sas" enclose="none">log(base)</source></big>, where <big><source lang="sas" enclose="none">log()</source></big> is the natural logarithm function.  
+
Sometimes, we need to calculate log value of any positive base number, just as in [[Excel]] or some other programming environment, we have function like <big><source lang="sas" enclose="none">log(Val, Base)</source></big>. It is not hard to do so by noting that we can calculate logarithms with other base values by dividing the result of the <big><source lang="sas" enclose="none">log()</source></big> function with <big><source lang="sas" enclose="none">log(base)</source></big>, where <big><source lang="sas" enclose="none">log()</source></big> is the natural logarithm function.  
  
 
Therefore, <big><source lang="sas" enclose="none">log(Value, Base)=log(Value)/log(Base)</source></big>.  This can be shown in this short [[DATA step]] to the right.
 
Therefore, <big><source lang="sas" enclose="none">log(Value, Base)=log(Value)/log(Base)</source></big>.  This can be shown in this short [[DATA step]] to the right.
  
<noinclude>
+
{{clear}}
[[Category:Base SAS]]
+
[[Category:LOG Function]]
+
</noinclude>
+
 
+
 
<!-- Please do not edit below this line, EXCEPT when promoting a tip -->
 
<!-- Please do not edit below this line, EXCEPT when promoting a tip -->
 
<div style="float:right">Submitted By [[User:http&#58;//sas-programming.blospot.com|Liang Xie]]</div>
 
<div style="float:right">Submitted By [[User:http&#58;//sas-programming.blospot.com|Liang Xie]]</div>
 +
 
<noinclude>
 
<noinclude>
 +
[[Category:Base SAS]]
 +
[[Category:LOG Function]]
 
[[Category:Tip in Use]]
 
[[Category:Tip in Use]]
 
</noinclude>
 
</noinclude>

Latest revision as of 12:04, 13 September 2017

data show;
    val = 55;
    b2 = log2(val);
    v2 = log(val) / log(2);
 
    put b2= ;
    put v2=;
run;

SAS supports only 3 bases for the log function, namely, {2, 10, exp(1)}, corresponding to functions log2(), log10(), and log(), respectively.

Sometimes, we need to calculate log value of any positive base number, just as in Excel or some other programming environment, we have function like log(Val, Base). It is not hard to do so by noting that we can calculate logarithms with other base values by dividing the result of the log() function with log(base), where log() is the natural logarithm function.

Therefore, log(Value, Base)=log(Value)/log(Base). This can be shown in this short DATA step to the right.


Submitted By Liang Xie