I have a character variable with a value like this (see below), that has some weird looking quotes around it. “ ”
All I want to do is get a new character variable called B11 and make it's value b when the response is Would you like a plant in your room?
I've tried a lot of different things, but the B11 value always ends up blank/no value.
The response_13 variable length in bytes is 104 and it's format is $CHAR89.
The B11 variable has a length of 1.
The response_13c variable also has a length in bytes of 104.
Here is my code:
response_13b=strip(response_13);
response_13c=compress(response_13b,"“”");
if response_13c="Would you like a plant in your room?"
then B11="b";
I'm using SAS Enterprise Guide.
I'm thoroughly frustrated, please help!
The compress function does seem to take off the weird quotes, so I don't know why B11 still has a blank value.
You could ignore the "stupid" quotes and instead just test if the text is anywhere in the variable.
if index(response_13c,"Would you like a plant in your room?") then B11="b";
You could use compress to remove the actual stupid quote characters. You could try copy and pasting them into your code.
response_13c = strip(compress(response_13,'“”');
Or lookup the hex codes and use hex literals instead, but note that the hex codes used by those non standard ASCII characters could depend on what encoding your SAS session is using. This will work with LATIN1 encoding:
response_13c = strip(compress(response_13,'9394'x);
But is you are using UTF-8 encoding then you will need
'E2809CE2809D'x
instead.
Also watch out as some of the other things that look like spaces in the photograph you posted might not actually be spaces. They could be "non breaking" spaces ('A0'x) or perhaps tabs ('09'x) or linefeeds ('0A'x) or carriage returns ('0D'x).
PS Your variable definition seems messed up. It is defined to store 104 bytes but has a format attached that will only show the first 89 bytes of the value. Perhaps you should just remove the format as formats are not normally needed for character variables. SAS does not need to be given special instructions for how to display character variables.
You could ignore the "stupid" quotes and instead just test if the text is anywhere in the variable.
if index(response_13c,"Would you like a plant in your room?") then B11="b";
You could use compress to remove the actual stupid quote characters. You could try copy and pasting them into your code.
response_13c = strip(compress(response_13,'“”');
Or lookup the hex codes and use hex literals instead, but note that the hex codes used by those non standard ASCII characters could depend on what encoding your SAS session is using. This will work with LATIN1 encoding:
response_13c = strip(compress(response_13,'9394'x);
But is you are using UTF-8 encoding then you will need
'E2809CE2809D'x
instead.
Also watch out as some of the other things that look like spaces in the photograph you posted might not actually be spaces. They could be "non breaking" spaces ('A0'x) or perhaps tabs ('09'x) or linefeeds ('0A'x) or carriage returns ('0D'x).
PS Your variable definition seems messed up. It is defined to store 104 bytes but has a format attached that will only show the first 89 bytes of the value. Perhaps you should just remove the format as formats are not normally needed for character variables. SAS does not need to be given special instructions for how to display character variables.
The INDEX() function returns the location in the first string where the second string starts. If it is not found it returns zero.
SAS interprets any number that is not missing or zero as TRUE when used in a boolean expression, like the condition test of an IF/THEN statement.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.