Showing posts with label SAS variables. Show all posts
Showing posts with label SAS variables. Show all posts

Feb 12, 2012

Length and Precision of SAS Variables....

Did you know that ?
- The maximum number of variables in a single SAS data set under Windows is 32,767.
- An observation under Windows cannot be longer than 5MB(ie 5*1024*1024).
- Therefore, if you want your data set to contain 32,767 character variables, the longest each variable can be is approximately 160 bytes.
- However, a DATA step can reference more than 32,767 variables, if you write only 32,767 or fewer variables to the data set. For example, you could drop some variables with a DROP= data set option. The maximum number of variables a DATA step can reference under Windows is 2,147,483,647.
- The default length of numeric variables in SAS data sets is 8 bytes. (You can control the length of SAS numeric variables with the LENGTH statement in the DATA step.) In SAS under Windows, the Windows data type of numeric values that have a length of 8 is LONG REAL. The precision of floating-point values is always accurate to 15 digits. Depending upon the number, the precision may be 16 digits of accuracy.
Significant Digits and Largest Integer by Length for SAS Variables under Windows
Length in BytesLargest Integer Represented ExactlyExponential NotationSignificant Digits Retained
38,1922133
42,097,1522216
5536,870,9122298
6137,438,953,47223711
735,184,372,088,83224513
89,007,199,254,740,99225315
This maximum number varies for every Operating System…
72057594037927936 – os/390 IBM Mainframes
9007199254740991 – OpenVMS
9007199254740992 – Windows / Unix
For example, if you know that a numeric variable always has values between 0 and 100, you can use a length of 3 to store the number and thus save space in your data set.
Here is an example:
Trying out numbers greater than max number for len 8 in the above table to see the results….employed best32. format.
data numbers;
input x;
format x best32.;
cards;
9007199254740992
9007199254740999
90071992547409921
9007199254740992123
;
run;
Look at the output for obs 3 & 4…The values are getting rounded off to the next number with trailing zeroes…
sas lengthnum
Note: Dummy variables (those whose only purpose is to hold 0 or 1) can be stored in a variable whose length is 3 bytes.

Feb 9, 2010

Identify SAS Variable values prefixed / starting with an alphabet...Datastep and SQL methods...

In SAS, Inorder to identify SAS Variables starting with an alphabet one can use the colon modifiers (i.e. =: ‘Alphabet’, >: ‘Alphabet’ and <: lphabet="" span="">) to test for character strings prefixed with the alphabet.
For example...
 
/*Eg. 3 records starting with J for char variable a1*/
 
data example;
length a1 a2 a3 a4 $ 10;
input a1 a2 a3 a4;
cards;
Johnson Ketan Mike Kites
John Sawyer Sharon Michael
Joe Kitten Sue Lula
Sharon Michael SAS Doug
Pamela Tiger Woods Peyton;
 
run;
 
In a Data step...
/* We can use the : to identify obs for which a1 starts with J */
 
data finder1;
set example;
if a1=:'J'; /*keep obs whose a1 values start with J*/
run;
 
 
In a Proc step...
 
proc print data=example;
var a:; /*display variables that start with 'a' i.e. a1,a2,a3,a4*/
where a1=:'J';/*keep obs whose a1 values start with J*/
In a SQL step…
However in SQL, the datastep colon modifiers (i.e. =: ‘Alphabet’, >: ‘Alphabet’ and <: lphabet="" span="">‘)  do not work….But fortunately SAS provided string comparison operators as below to achieve similar tasks…
Truncated string comparison operators are used to compare two strings. They differ from conventional comparison operators in that, before executing the comparison, PROC SQL truncates the longer string to be the same length as the shorter string. The truncation is performed internally; neither operand is permanently changed. The following table lists the truncated comparison operators:

Truncated String Comparison Operators
SymbolDefinitionExample
EQTequal to truncated stringswhere Name eqt ‘Aust’;
GTTgreater than truncated stringswhere Name gtt ‘Bah’;
LTTless than truncated stringswhere Name ltt ‘An’;
GETgreater than or equal to truncated stringswhere Country get ‘United A’;
LETless than or equal to truncated stringswhere Lastname let ‘Smith’;
NETnot equal to truncated stringswhere Style net ‘TWO’;
/* The same as above but with eqt comparison operators */
 
proc sql;
create table finder2 as
select * from example
where a1 eqt 'J'; /*keep obs whose a1 values start with J*/
quit;