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

I have a character variable with a value like this (see below), that has some weird looking quotes around it.   

 

hein68_0-1659390867073.png

 

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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.

 

hein68
Obsidian | Level 7
Thanks very much! The line of code below worked for me. I am VERY grateful for your help!


if index(response_13c,"Would you like a plant in your room?") then B11="b";

If you can explain what this line of code is doing that would be helpful.
Tom
Super User Tom
Super User

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.

hein68
Obsidian | Level 7
Thanks, this is very helpful!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 4 replies
  • 504 views
  • 0 likes
  • 2 in conversation