BookmarkSubscribeRSS Feed
1239
Calcite | Level 5

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;  

 

 

5 REPLIES 5
Ksharp
Super User

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;
Patrick
Opal | Level 21

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;

Patrick_0-1666488868631.png

 

Tom
Super User Tom
Super User

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

FreelanceReinh
Jade | Level 19

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 668 views
  • 1 like
  • 6 in conversation