Hi SAS UE users,
I want to make a report table with "±" sign, but sas report as "
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;
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)));
In SAS Studio, your code works properly and produces the expected output.
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.
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.
Elizabeth
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.
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)));
Thank you all the suggestions!
Could you explain more what the x means in "c2b1'x? How can I add space before and after the plus/minus sign?
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.