Dynamic Currency Converting Formats
Converting currencies in SAS can often be a struggle. We can create our own formats that use up to date exchange rates to convert between any currencies worldwide.
SAS currently has built in formats such as EURTOGPB or the EUROCURR function that will convert money from one currency to another. The limitations of both of these methods are that they only apply to European currencies and the exchange rate is set based on the rate that was in place when the software was released. This rate can be modified by pointing to a text file containing the latest rates. This technique is still limited as it requires the text file to be manually updated and stored and still doesn’t enable EUR to USD conversion.
The program attached downloads live exchange rates for converting from EUROS from an external source and matches these with the currencies ISO code such as GBP.
The next part of the program creates a multiplier format for each currency for converting to and from EUROS, these formats are then stored in the work library.
Once you run the attached program, the example code below tests the format for converting to USD and back to EUROS. The formats are in the form EUROTO and EUROFR as EURTO are SAS current formats.
Testing the new formats:
*Converting to USD from EUROS and vice-versa; data _null_; euros=1000; usd=left(put(euros,eurotousd.)); *Because the result is returned to character the value has to be converted back to a numeric value; euros_back=left(put(input(usd,comma8.2),eurofrusd.)); put 'A ' euros ' EUROS will give you ' usd 'US Dollars'; put 'A ' usd ' US Dollars will give you ' euros_back 'EUROS'; run;
The SAS Log:
125 *Converting to USD from EUROS and vice-versa; 126 data _null_; 127 euros=1000; 128 usd=left(put(euros,eurotousd.)); 129 *Because the result is returned to character the 130 value has to be converted back to a numeric value; 131 euros_back=left(put(input(usd,comma8.2),eurofrusd.)); 132 put 'A ' euros ' EUROS will give you ' usd 'US Dollars'; 133 put 'A ' usd ' US Dollars will give you ' euros_back 'EUROS'; 134 run;
A 1000 EUROS will give you 1,277.80 US Dollars A 1,277.80 US Dollars will give you 1,000.00 EUROS NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
For more information view the SAS help entries or the SAS support website (www.support.sas.com).