BookmarkSubscribeRSS Feed
n6
Quartz | Level 8 n6
Quartz | Level 8
I'm doing something like

if x = 'abc' then y = 1;

But one of the things I'm checking for has an apostrophe in it. So it is

if x = 'Don't Know' then y=1;

But of course I can't do that because that gives me unbalanced quotes. So instead i tried

if x = "Don't Know" then y=1;

I don't get unbalanced quotes or an error when I use that, but the condition x="Don't Know" is never satisfied (even though it should be sometimes) and therefore I never get y=1 assigned.

Any ideas on what code to use so that I do get y=1 assigned when x is Don't Know?
6 REPLIES 6
Paige
Quartz | Level 8
Without seeing your data that it is checking, there is no way to tell what the cause could be. Your last statement is syntactically correct.

But here's a guess anyway.

Use trim(x) instead of x to eliminate trailing blanks from causing the assignment statement to fail. Message was edited by: Paige
n6
Quartz | Level 8 n6
Quartz | Level 8
Here is the exact code. It is part of a macro but that shouldn't matter, especially since I have other similiar macros that work fine. And the code below works fine too except for the line with Don't Know" in it. (Yes, it's stupid to have to do this task in the first place, but it's a long story as to why I have to.)

data x;
set final;

if &VAR = 'Strongly Agree' then &VAR = '1_' || 'Strongly Agree' ;
if &VAR = 'Agree' then &VAR = '2_' || 'Agree' ;
if &VAR = 'Disagree' then &VAR = '3_' || 'Disagree' ;
if &VAR = 'Strongly Disagree' then &VAR = '4_' || 'Strongly Disagree';
if &VAR = "Don't Know" then &VAR = '5_' || "Don't Know" ;
if &VAR = 'No answer' then &VAR = '6_' || 'No answer' ;

run;
deleted_user
Not applicable
could be the macro causing the problem, if this (Don't Know) is the only time you use/generate double quotes.

better evidence would help diagnosis.
How about providing a SAS log snippet with symbolgen, mprint and mlogic.



PeterC
n6
Quartz | Level 8 n6
Quartz | Level 8
Here is the macro and the log results for it when using mlogic, etc.

1265 %MACRO VAL_SA_A_D_SD_DK_No_a (var);
1266 data x;
1267 set final;
1268 if &VAR = 'Strongly Agree' then &VAR = '1_' || 'Strongly Agree' ;
1269 if &VAR = 'Agree' then &VAR = '2_' || 'Agree' ;
1270 if &VAR = 'Disagree' then &VAR = '3_' || 'Disagree' ;
1271 if &VAR = 'Strongly Disagree' then &VAR = '4_' || 'Strongly Disagree';
1272 if &VAR = "Don't Know" then &VAR = '5_' || "Don't Know" ;
1273 if &VAR = 'No answer' then &VAR = '6_' || 'No answer' ;
1274 run;
1275 proc freq data=final; tables &VAR; run;
1276 proc freq data=x ; tables &VAR; run;
1277 %MEND VAL_SA_A_D_SD_DK_No_a;
1278 %VAL_SA_A_D_SD_DK_No_a (S1_D1 );
MLOGIC(VAL_SA_A_D_SD_DK_NO_A): Beginning execution.
MLOGIC(VAL_SA_A_D_SD_DK_NO_A): Parameter VAR has value S1_D1
WARNING: The Base Product product with which DATASTEP is associated will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Most typically this warning period runs for 45 days. Please run PROC SETINIT to
obtain more information on your warning period.
MPRINT(VAL_SA_A_D_SD_DK_NO_A): data x;
MPRINT(VAL_SA_A_D_SD_DK_NO_A): set final;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = 'Strongly Agree' then S1_D1 = '1_' || 'Strongly Agree' ;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = 'Agree' then S1_D1 = '2_' || 'Agree' ;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = 'Disagree' then S1_D1 = '3_' || 'Disagree' ;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = 'Strongly Disagree' then S1_D1 = '4_' || 'Strongly Disagree';
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = "Don't Know" then S1_D1 = '5_' || "Don't Know" ;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): if S1_D1 = 'No answer' then S1_D1 = '6_' || 'No answer' ;
MPRINT(VAL_SA_A_D_SD_DK_NO_A): run;

NOTE: There were 115 observations read from the data set WORK.FINAL.
NOTE: The data set WORK.X has 115 observations and 107 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds


WARNING: The Base Product product with which FREQ is associated will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Most typically this warning period runs for 45 days. Please run PROC SETINIT to
obtain more information on your warning period.
MPRINT(VAL_SA_A_D_SD_DK_NO_A): proc freq data=final;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): tables S1_D1;
MPRINT(VAL_SA_A_D_SD_DK_NO_A): run;

NOTE: There were 115 observations read from the data set WORK.FINAL.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


WARNING: The Base Product product with which FREQ is associated will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Most typically this warning period runs for 45 days. Please run PROC SETINIT to
obtain more information on your warning period.
MPRINT(VAL_SA_A_D_SD_DK_NO_A): proc freq data=x ;
SYMBOLGEN: Macro variable VAR resolves to S1_D1
MPRINT(VAL_SA_A_D_SD_DK_NO_A): tables S1_D1;
MPRINT(VAL_SA_A_D_SD_DK_NO_A): run;

NOTE: There were 115 observations read from the data set WORK.X.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


MLOGIC(VAL_SA_A_D_SD_DK_NO_A): Ending execution.

And here is the generated output. The prefix numbers get assigned for all the other groups but not for the Don't Know group.

Cumulative Cumulative
S1_D1 Frequency Percent Frequency Percent
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1_Strongly Agree 23 20.91 23 20.91
2_Agree 31 28.18 54 49.09
3_Disagree 23 20.91 77 70.00
4_Strongly Disagree 4 3.64 81 73.64
Don’t Know 29 26.36 110 100.00

Frequency Missing = 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Based on what you provided thus far, I could not recreate the problem, with a SAS 9.1.3 SP4 environment on Windows, with a manually assigned S1_D1 initial value in a RETAIN statement.

You have an interesting challenge - continue to presume that there is something in your data or code. Suggest you add some PUTLOG _ALL_; statements before and then again after the re-assignment statements. Also, consider using ELSE "condition" tests so that only one statement test will pass, if any. Appears as though you will need to diagnose the problem further within your DATA step -- as suggested with adding PUTLOG statements along the way, at various code points.

Scott Barry
SBBWorks, Inc.
Flip
Fluorite | Level 6
You may want to use the RANK function to examine the apostrophe to make sure it is exactly the same character. Otherwise split the word and check for the substring before and after the apostrophe. Message was edited by: Flip

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 909 views
  • 0 likes
  • 5 in conversation