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

Hi SAS UE users,

 

I want to make a report table with "±" sign, but sas report as "

ERROR: Invalid characters were present in the data.
ERROR: An error occurred while processing text data."
 
I have tried to find information and I only found this link might be relevant "https://www.lexjansen.com/phuse/2016/pp/PP10.pdf". 
 
However, it mentions the use of byte to deal with the problem but it does not work for me. 
 
I appreciate any suggestions. 
 
data sign;
a = 10;
b = 20;
%LET PLUSMIN=%SYSFUNC(BYTE(177)) ;
c = trim(left(put(a,10.2))) || " &PLUSMIN " || trim(left(put(b,10.2)));
run;


proc report data = sign;
run;
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ehbales
SAS Employee

My apologies. I assumed that adding the BYTE function to the code was a workaround when a file with the actual character was opened into SAS Studio. If the encoding of your SAS session is WLATIN1, I agree that the code you have should work. However, since you are seeing the errors about invalid characters present in the data, there is a possibility that the encoding of your SAS session is UTF-8. Run this to verify.

 

PROC OPTIONS OPTION=ENCODING; RUN:

 

The ASCII characters (7-bit) are represented as one byte in UTF-8. All other characters, including the ±, must be represented as more than one byte. Since the BYTE function only returns one byte, the value for 177 is invalid in UTF-8. That could cause the errors you see. 

 

The UTF-8 hexadecimal representation for ± is C2B1. If your SAS session encoding is UTF-8, you can use the following statement to add that character to the string directly:

 

c = trim(left(put(a,10.2))) || 'c2b1'x || trim(left(put(b,10.2)));

 

You could also use KPROPDATA to convert the result of BYTE to UTF-8. That's slightly more complicated but looks like this:

 

c = trim(left(put(a,10.2))) || KPROPDATA(BYTE(177), 'uesc', 'wlatin1', 'utf8') || trim(left(put(b,10.2)));

 

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

In SAS Studio, your code works properly and produces the expected output.

 

Capture.PNG

 

There may be something wrong with your installation of UE or SAS Studio. Or perhaps you are using a different output destination. What output destination are you using? What is your version of SAS?

 

By the way, no need for macro variables or macro functions here.

--
Paige Miller
ehbales
SAS Employee

If the SAS code contains the ± character, and the file was saved using your native Windows encoding for English, then the ± character is represented as one byte. By default, SAS Studio expects the encoding of files to be UTF-8 unless you have specified a default text encoding other than UTF-8. However, the ± character is not a valid one-byte character in UTF-8, so the character is converted to the the the UTF-8 replacement character.

 

This blog post has suggestions for informing SAS Studio that your SAS file was created using the native WIndows encoding.

https://blogs.sas.com/content/sgf/2018/06/22/a-transcoding-story-or-how-oliver-s-fusling-lost-his-la...

 

Elizabeth 

PaigeMiller
Diamond | Level 26

If the SAS code contains the ± character,


I'm confused. The SAS code presented does not contain that character. So I don't see how this applies.

--
Paige Miller
ehbales
SAS Employee

My apologies. I assumed that adding the BYTE function to the code was a workaround when a file with the actual character was opened into SAS Studio. If the encoding of your SAS session is WLATIN1, I agree that the code you have should work. However, since you are seeing the errors about invalid characters present in the data, there is a possibility that the encoding of your SAS session is UTF-8. Run this to verify.

 

PROC OPTIONS OPTION=ENCODING; RUN:

 

The ASCII characters (7-bit) are represented as one byte in UTF-8. All other characters, including the ±, must be represented as more than one byte. Since the BYTE function only returns one byte, the value for 177 is invalid in UTF-8. That could cause the errors you see. 

 

The UTF-8 hexadecimal representation for ± is C2B1. If your SAS session encoding is UTF-8, you can use the following statement to add that character to the string directly:

 

c = trim(left(put(a,10.2))) || 'c2b1'x || trim(left(put(b,10.2)));

 

You could also use KPROPDATA to convert the result of BYTE to UTF-8. That's slightly more complicated but looks like this:

 

c = trim(left(put(a,10.2))) || KPROPDATA(BYTE(177), 'uesc', 'wlatin1', 'utf8') || trim(left(put(b,10.2)));

 

 

superbibi
Obsidian | Level 7

Thank you all the suggestions!

superbibi
Obsidian | Level 7

Could you explain more what the x means in "c2b1'x? How can I add space before and after the plus/minus sign?

ehbales
SAS Employee

The string followed by the x is a SAS character constant that is represented in hexadecimal notation. I've included a link to the SAS documentation. There is a brief explanation of hex constants there.

 

https://go.documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=lrcon&docsetTarget=p0c... 

 

The sequence of characters within the string are the internal representation, or code points, of the character(s) that you want to represent. Hexadecimal notation is convenient if the character you want in the sting is not present on your keyboard. If you want to add quotes, you can do that one of the following ways:

 

1. Hex character constants can include one or more characters. The internal representation of the space character is Hex 20. You could add spaces using the following syntax:

 

   c = trim(left(put(a,10.2))) || '20c2b120'x || trim(left(put(b,10.2)));

 

2. You can concatenate the spaces.

 

   c = trim(left(put(a,10.2))) || ' ' || 'c2b1'x || ' ' || trim(left(put(b,10.2)));

 

Note: As a reminder, C2B1 is the Hexadecimal representation of the ± character in UTF-8. 

superbibi
Obsidian | Level 7
Thank you very much!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 5878 views
  • 2 likes
  • 3 in conversation