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.
There is value ' Master's Degree' which will create an error because of the symbol '.
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&' 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;
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;
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;
Oh just simple as that, haha! Thanks a lot, it worked.
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.
Do this instead (Double quotes)
Else If Qu_10 = "Master’s Degree" then Qu_10_num=4;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.