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:Create an Informat from a User-Defined Function"

From sasCommunity
Jump to: navigation, search
(wiki gardening - fixed category tags)
(contents shortened to "TIP length")
Line 5: Line 5:
  
 
<source lang="sas">
 
<source lang="sas">
* create a function named FRACT in the work library;
+
* create a FUNCTION named FRACT in the work library;
 
proc fcmp outlib=work.functions.math;  
 
proc fcmp outlib=work.functions.math;  
 
function fract(x $);  
 
function fract(x $);  
a = input(scan(x,1),best.);
+
return( sum( input(scan(x,1),best.) , input(scan(x,2),best.) / input(scan(x,3),best.) ));
b = input(scan(x,2),best.);
+
c = input(scan(x,3),best.);
+
return(sum( a, b/c));  
+
 
endsub;  
 
endsub;  
 
run;  
 
run;  
Line 17: Line 14:
  
 
options cmplib=work.functions;  
 
options cmplib=work.functions;  
</source>
 
 
Now try the function ... convert character fractions to numeric values.
 
 
<source lang="sas">
 
data charvar;
 
input x $10.;
 
datalines;
 
5 1/2
 
8
 
6 1/3
 
100 1/4
 
99
 
run;
 
 
data numvar;
 
set charvar;
 
y = fract(x);
 
run;
 
</source>
 
 
Data set NUMVAR looks as follows ...
 
 
<pre>
 
x            y
 
5 1/2        5.500
 
8            8.000
 
6 1/3        6.333
 
100 1/4    100.250
 
99          99.000
 
</pre>
 
 
Now use the function in PROC FORMAT to create an INFORMAT.
 
  
<source lang="sas">
+
* create an INFORMAT from the FUNCTION;
 
proc format;  
 
proc format;  
 
invalue fract other=[fract()];  
 
invalue fract other=[fract()];  
 
run;  
 
run;  
  
 +
* try the INFORMAT;
 
data x;  
 
data x;  
input x fract.;  
+
input x : & fract. @@;
 +
put x= @@;
 
datalines;  
 
datalines;  
5 1/2  
+
5 1/2   8   100 1/4   99   18 2/7
8  
+
6 1/3
+
100 1/4  
+
99  
+
 
;  
 
;  
 
run;  
 
run;  
 +
 +
in the LOG ...
 +
 +
x=5.5 x=8 x=100.25 x=99 x=18.285714286
 
</source>
 
</source>
  
Data set X looks as follows ...
 
  
<pre>
 
  x
 
  5.500
 
  8.000
 
  6.333
 
100.250
 
99.000
 
</pre>
 
 
<!-- Insert any appropriate category tags. Note that it is important to put the categorys inside the <noinclude> block so the categories are only applied to the tip page.
 
<!-- Insert any appropriate category tags. Note that it is important to put the categorys inside the <noinclude> block so the categories are only applied to the tip page.
 
- You can have as many CATEGORY tag lines as you want.
 
- You can have as many CATEGORY tag lines as you want.

Revision as of 15:56, 1 October 2011

Did you know that in version 9.3, you can create your own INFORMAT from a user-defined function? Here's an example.

Say you have raw data that contains numbers that are sometimes expressed as fractions. First you create a function that converts the fractions to decimal values. Then you create an INFORMAT from that function.

* create a FUNCTION named FRACT in the work library;
proc fcmp outlib=work.functions.math; 
function fract(x $); 
return( sum( input(scan(x,1),best.) , input(scan(x,2),best.) / input(scan(x,3),best.) ));
endsub; 
run; 
quit; 
 
options cmplib=work.functions; 
 
* create an INFORMAT from the FUNCTION;
proc format; 
invalue fract other=[fract()]; 
run; 
 
* try the INFORMAT;
data x; 
input x : & fract. @@; 
put x= @@;
datalines; 
5 1/2   8   100 1/4   99   18 2/7
; 
run; 
 
in the LOG ...
 
x=5.5 x=8 x=100.25 x=99 x=18.285714286

....see also

Submitted By Mike Zdeb