DATA Step, Macro, Functions and more

Encoding when reading data from api

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Encoding when reading data from api

How do I get the correct encoding for this data i am retrieving form with an api? The Danish letters æ,ø,å does not show corret

 

filename src temp encoding="utf-8";
proc http
     method="GET"
     url="%nrstr(https://dawa.aws.dk/adresser?q=Ærøgade&struktur=mini&per_side=1&format=csv)"
     out=src;
    run;
	data _null_;
	infile src;
	input;
	put _infile_;
	run;
	data test ;
	infile src encoding="utf-8" dlm=',' missover dsd firstobs=2 ;
	length id $ 36 status $1 vejkode $4 vejnavn $20 adresseringsvejnavn $20 husnr $10 etage $10 dor $10 supplerendebynavn $10 postnr $4 postnrnavn $20 kommunekode $4 adgangsadresseid $36 x 8 y 8 ;  
   input id $ status $ vejkode $ vejnavn $ adresseringsvejnavn $ husnr $ etage$ dor $ supplerendebynavn $ postnr $ postnrnavn $ kommunekode $ adgangsadresseid x y;
run;

Data example

id,status,vejkode,vejnavn,adresseringsvejnavn,husnr,etage,dør,supplerendebynavn,postnr,postnrnavn
,kommunekode,adgangsadresseid,x,y
0a3f50b3-135b-32b8-e044-0003ba298018,1,0365,Ærøgade,Ærøgade,2,,,,5960,Marstal,0492,0a3f5088-8b
75-32b8-e044-0003ba298018,10.51420646,54.85857924

Accepted Solutions
Solution
‎02-08-2018 03:30 AM
Occasional Contributor
Posts: 13

Re: Encoding when reading data from api

I found a solution. Basically you save the file with utf-8 and then open it with wlatin.

 

filename resp "C:\Temp\map.json";
    filename minmap 'C:/Temp/gogle_distance.map';

    proc http
     url="https://maps.googleapis.com/maps/api/directions/json?origin=&home.%nrstr(&destination=)&work.%nrstr(&key=MA4W-OEM-500K)"
     method= "GET"
     out=resp;
    run;

    %** trick der gøre at JSON filen encodes med wlatin;
    filename resp "C:\Temp\map.json" encoding='utf-8';
    filename resp1 "C:\Temp\map1.json" encoding='wlatin1';

    data _null_;
      infile resp;
      file resp1;
      input;
      put _infile_;
    run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,222

Re: Encoding when reading data from api

What is your SAS session encoding?  This is normally setup when SAS starts up, you would need to change it there.

Occasional Contributor
Posts: 13

Re: Encoding when reading data from api

That is not possible. I need a solution where the encoding is converted to my sas encoding,

Super User
Super User
Posts: 9,222

Re: Encoding when reading data from api

I don't believe that is possible.  If your SAS session is Wlatin, and the data coming in has UTF-8 or 16, which both have far more characters in their sets, then "converting" data is not possible as the wlatin charset is too small to handle it.  This is what you are seeing now, where the character set just says I don't know and puts a strange symbol.

Occasional Contributor
Posts: 13

Re: Encoding when reading data from api

I am not talking about converting the whole charset to from utf-8 to wlatin. Only those that are in wlatin

Solution
‎02-08-2018 03:30 AM
Occasional Contributor
Posts: 13

Re: Encoding when reading data from api

I found a solution. Basically you save the file with utf-8 and then open it with wlatin.

 

filename resp "C:\Temp\map.json";
    filename minmap 'C:/Temp/gogle_distance.map';

    proc http
     url="https://maps.googleapis.com/maps/api/directions/json?origin=&home.%nrstr(&destination=)&work.%nrstr(&key=MA4W-OEM-500K)"
     method= "GET"
     out=resp;
    run;

    %** trick der gøre at JSON filen encodes med wlatin;
    filename resp "C:\Temp\map.json" encoding='utf-8';
    filename resp1 "C:\Temp\map1.json" encoding='wlatin1';

    data _null_;
      infile resp;
      file resp1;
      input;
      put _infile_;
    run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 107 views
  • 0 likes
  • 2 in conversation