This post was kindly contributed by SAS Users - go there to comment and to read the full post. |
Locale-specific SAS® format catalogs make reporting in multiple languages more dynamic. It is easy to generate reports in different languages when you use both the LOCALE option in the FORMAT procedure and the LOCALE= system option to create these catalogs. If you are not familiar with the LOCALE= system option, see the “Resources” section below for more information.
This blog post, inspired by my work on this topic with a SAS customer, focuses on how to create and use locale-specific informats to read in numeric values from a Microsoft Excel file and then transform them into SAS character values. I incorporated this step into a macro that transforms ones and zeroes from the Excel file into meaningful information for multilingual readers.
Getting started: Creating the informats
The first step is to submit the LOCALE= system option with the value fr_FR. For the example in this article, I chose the values fr_FR and en_US for French and English from this table of LOCALE= values. (That is because I know how to say “yes” and “no” in both English and French — I need to travel more!)
options locale=fr_fr;
|
The following code uses both the INVALUE statement and the LOCALE option in PROC FORMAT to create an informat that is named $PT_SURVEY:
proc format locale library=work; invalue $pt_survey 1='oui' 0='non'; run; |
Now, toggle the LOCALE= system option and create a second informat using labels in a different language (in this example, it is English):
options locale=en_us;
proc format locale library=work; invalue $pt_survey 1='yes' 0='no'; run; |
In the screenshot below, which shows the output from the DATASETS procedure, you can see that PROC FORMAT created two format catalogs using the specified locale values, which are preceded by underscore characters. If the format catalogs already exist, PROC FORMAT simply adds the $PT_SURVEY informat entry type to them.
proc datasets memtype=catalog; quit; |
Before you use these informats for a report, you must tell SAS where the informats are located. To do so, specify /LOCALE after the libref name within the FMTSEARCH= system option. If you do not add the /LOCALE specification, you see an error message stating either that the $PT_SURVEY informat does not exist or that it cannot be found. In the next two OPTIONS statements, SAS searches for the locale-specific informat in the FORMATS_FR_FR catalog, which PROC FORMAT created in the WORK library:
options locale=fr_fr; options fmtsearch=(work/locale); |
If you toggle the LOCALE= system option to have the en_US locale value, SAS then searches for the informat in the other catalog that was created, which is the FORMATS_EN_US catalog.
Creating the Excel file for this example
For this example, you can create an Excel file by using the ODS EXCEL destination from the REPORT procedure output. Although you can create the Excel file in various ways, the reason that I chose the ODS EXCEL statement was to show you some options that can be helpful in this scenario and are also useful at other times.
Use the ODS EXCEL destination to create a file from PROC REPORT. I specify the TAGATTR= style attribute using “TYPE:NUMBER” for the Q_1 variable:
%let path=%sysfunc(getoption(WORK)); filename temp "&path\surveys.xlsx"; ods excel file=temp; data one; infile datalines truncover; input ptID Q_1; datalines; 111 0 112 1 ; run; proc report data=one; define ptID / display style(column)={tagattr="type:String"}; define Q_1 / style(column)={tagattr="type:Number"}; run; ods excel close; |
Now you have a file that looks like this screenshot when it is opened in Excel. Note that the data value for the Q_1 column is numeric:
The IMPORT procedure uses the DBSASTYPE= data set option to convert the numeric Excel data into SAS character values. Then I can apply the locale-specific character informat to a character variable.
As you will see below, in the macro, I use DBMS=EXCEL in PROC IMPORT to read the Excel file because my SAS and Microsoft Office versions are both 64-bit. (You might have to use the PCFILES LIBNAME Engine to connect to Excel through the SAS PC Files Server if you are not set up this way.)
Using the informats in a macro to create the multilingual reports
The final step is to run the macro with parameters to produce the two reports in French and English, using the locale-specific catalogs. When the macro is called, depending on the parameter value for the macro variable LOCALE, the LOCALE= system option changes, and the $PT_SURVEY informat from the locale-specific catalog is applied. These two tabular reports are produced:
Here is the full code for the example:
%let path=%sysfunc(getoption(WORK)); filename temp "&path\surveys.xlsx"; ods excel file=temp; data one; infile datalines truncover; input ptID Q_1; datalines; 111 0 112 1 ; run; proc report data=one; define ptID / display style(column)={tagattr="type:String"}; define Q_1 / style(column)={tagattr="type:Number"}; run; ods excel close; options locale=fr_fr; proc format locale library=work; invalue $pt_survey 1='oui' 0='non'; run; options locale=en_us; proc format locale library=work; invalue $pt_survey 1='yes' 0='no'; run; /* Set the FMTSEARCH option */ options fmtsearch=(work/locale); /* Compile the macro */ %macro survey(locale,out); /* Set the LOCALE system option */ options locale=&locale; /* Import the Excel file */ filename survey "&path\surveys.xlsx"; proc import dbms=excel datafile=survey out=work.&out replace; getnames=yes; dbdsopts="dbsastype=(Q_1='char(8)')"; run; data work.&out; set work.&out; /* Create a new variable for the report whose values are assigned by specifying the locale-specific informat in the INPUT function */ newvar=input(Q_1, $pt_survey.); label newvar='Q_1'; run; options missing='0'; /* Create the tabular report */ proc tabulate data=&out; class ptID newvar; table ptID='Patient ID', newvar*n=' '/box="&locale"; run; %mend survey; /* Call the macros */ %survey(fr_fr,fr) %survey(en_us,en) |
For a different example that does not involve an informat, you can create a format in a locale-specific catalog to print a data set in both English and Romanian. See Example 19: Creating a Locale-Specific Format Catalog in the Base SAS® 9.4 Procedures Guide.
Resources
For more information about the LOCALE option:
- LOCALE System Option in SAS® 9.4 National Language Support (NLS): Reference Guide, Fifth Edition
- Internationalization 101: Give Some International Flavor to Your SAS® Applications
For more information about reading and writing Excel files:
- Tell SAS to read a database field as CHAR instead of INT or BIGINT
- Microsoft Excel Workbook Files in SAS/ACCESS® 9.4 Interface to PC Files: Reference, Fourth Edition
For more information about creating macros and using the macro facility in SAS:
- SAS® 9.4 Macro Language: Reference, Fifth Edition
- SAS Note 24451, “Creating an autocall macro library on a PC”
Using locale-specific format catalogs to create reports in multiple languages was published on SAS Users.
This post was kindly contributed by SAS Users - go there to comment and to read the full post. |