Hi All,
I have the below dataset with exponentiation values in variable 'a' and I want to filter the particular value (6.7705698E14
) in the dataset and how do I apply where condition or if statement to pull 6.7705698E14
?
Thanks for your help in advance!
data test;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
run;
data want;
set test;
where a = 6.7705698E14;
run;
Assuming you have specified number .
data test;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
run;
data want;
set test;
if int(a/1e7)=67705698;
run;
If this exponential data is stored in a numerical variable then it's just how the values get printed. You can always "attach" a different format to the variable so it prints differently. This doesn't change the internal storage of the value which also means you can express the value either with an exponent or just as digits.
data work.test;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
a=6.5693077E14; output;
a=6.7705698E14; output;
a=6.8283942E14; output;
run;
proc datasets lib=work nolist;
modify test;
format a f16.;
run;
quit;
proc print data=test;
run;
You can just type in the value, just like you did in the assignment statement.
where a=6.7705698E14;
But the real value probably has more digits that what you are seeing.
That looks like the output of the BEST12. format (which is what SAS normally uses when you haven't told it to use something else). So of the 12 characters 3 are being used for the exponent and one for the decimal point so you only have 8 digits. But 6 times 10 to the 14th power is 15 decimal digits.
8 data test; 9 input @1 a @1 string $20. ; 10 string2=put(a,32.-L); 11 length = length(string); 12 length2 = length(string2); 13 format a 20.; 14 put (_all_) (=/); 15 cards; a=677056980000000 string=6.7705698E14 string2=677056980000000 length=12 length2=15
So the real value might be any number in the range from
677,056,979,500,000 to
677,056,980,499,999
Hi @1239,
If those numbers (in your real data) are the results of calculations rather than hardcoded literals, checking exact equality ("if a= ...") is likely to fail. As Tom has pointed out, you would most probably miss relevant digits.
Example:
1200 data _null_; 1201 a=exp(4.25769*exp(2.082)); 1202 put a best12. / a best32.-L / a 18.1-L / a hex16.; 1203 if a ne 677056981379146 then put 'unequal'; 1204 if a=677056981379145.9 then put 'OK 1a'; 1205 if a=6.770569813791458E14 then put 'OK 1b'; 1206 if put(a,hex16.)='43033E3CE6F1424F' then put 'OK 2'; 1207 if round(a,1e7)=6.7705698e14 then put 'OK 3'; 1208 if put(a,best12.)='6.7705698E14' then put 'OK 4'; 1209 run; 6.7705698E14 677056981379146 677056981379146.0 43033E3CE6F1424F unequal OK 1a OK 1b OK 2 OK 3 OK 4
So, there exist numeric literals for which exact equality holds (677056981379145.9 and 6.770569813791458E14), but none of the common numeric formats (w.d, BESTw., Ew., etc.) will show you sufficiently many digits so that you could type them.* Only special formats such as HEX16., BINARY64. or RB8. reveal the full binary content of a numeric variable, which is amenable to exact equality checks. Otherwise you need to resort to tests of only approximate equality, either using numeric or character values (see the last two IF conditions above).
Knowing your data, you could abbreviate those approximate comparisons based on how close other values get to your target value. Each of the three IF or WHERE conditions below works for your sample data:
put(a,best7.)='6.77E14'
round(a,1e12)=6.77e14
abs(a-6.7e14)<1e13
(In theory, the COMPFUZZ function should work as well.)
* Edit: With the non-default setting options decimalconv=stdieee the 18.1 format does show 677056981379145.9.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.