2
votes

I have this macro that creates a json file,
but even though I have specified encoding='utf-8' bom I don't get an utf-8 file.

%macro json4datatables_useformat(ds,path,file,charvars,numvars)
    / 
    DES="json4datatables(ds,path,file,charvars,numvars)";

    /* creates a json with no headers
     * a bit like a csv without the first line
     * it takes thus less space
     * but you have to know which column is what
     */

    data _null_;
        length line $300;
        set &ds nobs=nobs end=end;
        file "&path.&file." encoding='utf-8' bom/**/ ;

        line = '[';

        %if &charvars ne %then %do;
            %do i=1 %to %sysfunc(countw(&charvars));
                %let charvar = %scan(&charvars, &i);
                %if &i ne 1 %then %do;
                    line = cats(line,',');
                %end;
                line = cats(line,'"',vvalue(&charvar),'"');
            %end;
        %end;
        %if &numvars ne %then %do;
            %do i=1 %to %sysfunc(countw(&numvars));
                %let numvar = %scan(&numvars, &i);
                line = catx(',',line,vvalue(&numvar));
            %end;
        %end;

        line = cats(line,']');

        if _n_=1 then put '{"data": [';
        if not end then put line +(-1) ',';
        else do;
            put line;
            put ']}';
        end;
        run;

%mend json4datatables_useformat;

I noticed because accents appear as strange charactes on the web page where I display the json file.
And the issue is solved simply by opening the json file in sublime text and doing File>Save with Encoding>UTF-8. (with BOM isn't necessary.)

Is there another way to force utf-8 encoding?

EDIT: I'm using SAS EG 7.1 , SAS 9.3 on windows.

1
I used your macro and exported data with some polish caracters. Notepad++ says that it is UTF8 and Chrome browser displays all characters properly. What is your SAS version?fl0r3k
I guess this means it's probably system-relatedstallingOne
What encoding is your SAS session using? It shouldn't matter, in theory, but I find sometimes it helps to be in a UTF-8 session instead of an ANSI session. Also, what version of SAS are you running?Joe
How can I find the encoding of the SAS session? I'm using SAS EG 7.1 , SAS 9.3 on windows.stallingOne
I had no problems with this having wlatin2 session and writing to utf8. It is something else. If I get something I will let you know. BTW nice macro :)fl0r3k

1 Answers

0
votes

Your browser doesn't have the appropriate character set and / or fonts to render your data.

To add the language you want to browse:

Control Panel -> Region and Language -> Keyboards and Languages -> Display language -> Install/uninstall languages