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

Hi, I wanted to create a new variable from my data set. I tried to use If - then - Else if statement to do this. However, there is one value in the original variable contains symbol '. Thus I cannot write the Else if statement for this value. 

 

Capture.PNG

 

There is value ' Master's Degree' which will create an error because of the symbol '. 

 

Capture1.PNG

 

I also tried to run IF-Else if-else statement but didn't work as well. Please help 😄 thanks a lot.

 

data hth1n18.data;
set hth1n18.data;
If Qu_10 = 'High School' then Qu_10_num = 1;
Else If Qu_10 = 'High School/Secondary School' then Qu_10_num=1;
Else If Qu_10 = 'High School/Secondary School&amp' then Qu_10_num =1;
Else If Qu_10 = 'Some College' then Qu_10_num=2;
Else If Qu_10 = 'Some College  ' then Qu_10_num=2;
Else If Qu_10 = 'Some College &a' then Qu_10_num=2;
Else If Qu_10 = 'Some College  ' then Qu_10_num=2;
Else If Qu_10 = 'College' then Qu_10_num=3;
Else If Qu_10 = 'College Degree' then Qu_10_num=3;
Else If Qu_10 = 'College/University Degree' then Qu_10_num=3;
Else If Qu_10 = 'College/University Degree&am' then Qu_10_num=3;
Else If Qu_10 = 'College/University Degree&nb' then Qu_10_num=3;
Else If Qu_10 = 'College/University Degree ' then Qu_10_num=3;
Else If Qu_10 = 'University' then Qu_10_num=3;
Else If Qu_10 = 'University Degree' then Qu_10_num=3;
Else If Qu_10 = 'Master' then Qu_10_num=4;
Else If Qu_10 = 'Master Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master's Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master’s Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master’s Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master’s Degree' then Qu_10_num=4;
Else If Qu_10 = 'Masters Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master’s Degree' then Qu_10_num=4;
Else If Qu_10 = 'Master¡¯s Degree' then Qu_10_num=4;
Else If Qu_10 = 'Doctoral Degree' then Qu_10_num=5;
Else If Qu_10 = 'Doctoral Degree &am' then Qu_10_num=5;
Else If Qu_10 = 'Doctoral Degree &nb' then Qu_10_num=5;
Else If Qu_10 = 1 then Qu_10_num = 1;
Else If Qu_10 = 2 then Qu_10_num = 2;
Else If Qu_10 = 3 then Qu_10_num = 3;
Else If Qu_10 = 4 then Qu_10_num=4;
Else If Qu_10 = 5 then Qu_10_num=5;
Else If Qu_10 = . then Qu_10_num=.;
Else Qu_10_num=4;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

When you have to put a single quote into a string, use double quotes around the string, and vice versa:

data test;
length x1 $50;
x1 = 'How about "this"?';
output;
x1 = "That's it!";
output;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

When you have to put a single quote into a string, use double quotes around the string, and vice versa:

data test;
length x1 $50;
x1 = 'How about "this"?';
output;
x1 = "That's it!";
output;
run;
Cassie2
Calcite | Level 5

Oh just simple as that, haha! Thanks a lot, it worked.

ballardw
Super User

And an old school alternative:

 

data test;
length x1 $50;
x1 = 'How about "this"?';
output;
x1 = 'That''s it!';
output;
run;

That is two single quotes in the middle that will resolve to a single in the value.

Which can be useful if you have macro related special characters & and % elsewhere in the value such as

data test;
length x1 $50;
x1 = 'John''s Feed &Grain';
output;
x1 = "John's Feed &Grain";
output;
run;

where the second assignment generates a warning in the log. If you happen to actually have a macro variable named GRAIN at the time you may get unexpected results.

PeterClemmensen
Tourmaline | Level 20

Do this instead (Double quotes)

 

Else If Qu_10 = "Master’s Degree" then Qu_10_num=4;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 680 views
  • 4 likes
  • 4 in conversation