BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
larsc
Obsidian | Level 7

Dear SAS community,

 

I am fairly new to SAS and am currently running into some issues with the response from a Get call made to a REST API. Specifically, I am having issues parsing the Scandinavian letters 'æ, ø, å'. This code runs in Data Integration Studio in a UWC transformation:

 

filename lcode temp encoding="wlatin1";
filename lcode1 temp encoding="utf-8";

/*Make Get Call*/
%macro getResp();

	proc http 
		url="https://fakepath/response"
 		method="GET"
	   out=lcode;

	   headers 
			'Content-Type' = 'application/json' 
         'Accept' = 'application/json'
    		'Authorization' = "Bearer &access_token.";
		run;

	 data _null_;
      infile lcode;
      file lcode1;
      input;
      put _infile_;
   run;


	%parseRoot();
	%parseRespType();

%mend;

/*Macro to parse root*/
%macro parseRoot();
	
	libname resp json fileref=lcode1;

	data temp1;
			set resp.Root;
	run;

%mend;

%macro parseRespType();	

	libname resp json fileref=lcode1;

	data temp2;
			set resp.RespType;
			where RespTypeName Like 'LCD%';
	run;

%mend;


%getResp();

/*Join respons from REST API*/
proc sql;
	CREATE TABLE &_output1 as
	SELECT RespName, RespValue
	FROM temp1 as t1 inner join temp2 as t2
	ON t1.ordinal_root = t2.ordinal_root
	ORDER BY RespName;
quit;

The code runs successfully and I receive the data as expected with the exception of words containing 'æ, ø, å' (e.g., løft -> løft). The reason why I've set up the code in the manner which I have is that I based this on the solution offered in this thread https://communities.sas.com/t5/SAS-Programming/Encoding-when-reading-data-from-api/td-p/426050 which does not work for me. Beyond changing the session encoding, is there a way to work around this issue? 

1 ACCEPTED SOLUTION

Accepted Solutions
larsc
Obsidian | Level 7

Thanks for your reply Chris - I'll try to provide more details. Firstly, the encoding of my SAS session is Latin1.

 

My issue specifically was parsing Scandinavian letters ('æ, ø, å') when getting data from a REST API. For instance, for the word 'Løft' the 'ø' character in DI studio gets converted to 'øf'. So, this is not what I want or expect - as ideally the letters 'æ, ø, å' would be preserved in words containing these letters.

 

While typing out this reply I actually managed to get it to work; the solution was to set up the code in the following manner which is exactly like the solution offered in this thread https://communities.sas.com/t5/SAS-Programming/Encoding-when-reading-data-from-api/td-p/426050 - previously I missed the first filename definition.

 

/*Workaround to parse special scandinavian characters*/
%let f_path = /fakepath./keyword.json;
filename lcode "&f_path";


/*Make Get Call*/
%macro getSugg();

	proc http 
		url="https://fakepath/constant"
 		method="GET"
	   out=lcode;

	   headers 
			'Content-Type' = 'application/json' 
         'Accept' = 'application/json'
    		'Authorization' = "Bearer &access_token.";
		run;

	%let f_path = /fakepath./keyword.json;
	filename lcode "&f_path" encoding = "utf-8";
				
	%let f_path = /fakepath/keyword1.json;
	filename lcode1 "&f_path" encoding="wlatin1";

	 data _null_;
      infile lcode;
      file lcode1;
      input;
      put _infile_;
   run;

	%parseRoot();
	%parseCONSTANTTYPE();

%mend;

/*Macro to parse root*/
%macro parseRoot();
	
	libname resp json fileref = lcode1;

	data temp1;
			set resp.Root;
	run;

%mend;

%macro parseCONSTANTTYPE();	

	libname resp json fileref = lcode1;

	data temp2;
			set resp.CONSTANTTYPE;
			where ConstantTypeName Like 'XYZ%';
	run;

%mend;


%getSugg();

/*Join respons from REST API*/
proc sql;
	CREATE TABLE &_output1 as
	SELECT ConstantTypeName, ConstantValue
	FROM temp1 as t1 inner join temp2 as t2
	ON t1.ordinal_root = t2.ordinal_root
	ORDER BY CConstantTypeName;
quit;

Big thanks to @rudfaden 

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

What is the encoding of your SAS session?

What exactly doesn't work? Give more details please.

Where does the process break? Are the contents of file LCODE valid? What about file LCODE1?

larsc
Obsidian | Level 7

Thanks for your reply Chris - I'll try to provide more details. Firstly, the encoding of my SAS session is Latin1.

 

My issue specifically was parsing Scandinavian letters ('æ, ø, å') when getting data from a REST API. For instance, for the word 'Løft' the 'ø' character in DI studio gets converted to 'øf'. So, this is not what I want or expect - as ideally the letters 'æ, ø, å' would be preserved in words containing these letters.

 

While typing out this reply I actually managed to get it to work; the solution was to set up the code in the following manner which is exactly like the solution offered in this thread https://communities.sas.com/t5/SAS-Programming/Encoding-when-reading-data-from-api/td-p/426050 - previously I missed the first filename definition.

 

/*Workaround to parse special scandinavian characters*/
%let f_path = /fakepath./keyword.json;
filename lcode "&f_path";


/*Make Get Call*/
%macro getSugg();

	proc http 
		url="https://fakepath/constant"
 		method="GET"
	   out=lcode;

	   headers 
			'Content-Type' = 'application/json' 
         'Accept' = 'application/json'
    		'Authorization' = "Bearer &access_token.";
		run;

	%let f_path = /fakepath./keyword.json;
	filename lcode "&f_path" encoding = "utf-8";
				
	%let f_path = /fakepath/keyword1.json;
	filename lcode1 "&f_path" encoding="wlatin1";

	 data _null_;
      infile lcode;
      file lcode1;
      input;
      put _infile_;
   run;

	%parseRoot();
	%parseCONSTANTTYPE();

%mend;

/*Macro to parse root*/
%macro parseRoot();
	
	libname resp json fileref = lcode1;

	data temp1;
			set resp.Root;
	run;

%mend;

%macro parseCONSTANTTYPE();	

	libname resp json fileref = lcode1;

	data temp2;
			set resp.CONSTANTTYPE;
			where ConstantTypeName Like 'XYZ%';
	run;

%mend;


%getSugg();

/*Join respons from REST API*/
proc sql;
	CREATE TABLE &_output1 as
	SELECT ConstantTypeName, ConstantValue
	FROM temp1 as t1 inner join temp2 as t2
	ON t1.ordinal_root = t2.ordinal_root
	ORDER BY CConstantTypeName;
quit;

Big thanks to @rudfaden 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5779 views
  • 0 likes
  • 2 in conversation