Help using Base SAS procedures

Apostrophes in assignment statements

Reply
Contributor n6
Contributor
Posts: 62

Apostrophes in assignment statements

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?
Super Contributor
Posts: 281

Re: Apostrophes in assignment statements

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
Contributor n6
Contributor
Posts: 62

Re: Apostrophes in assignment statements

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;
N/A
Posts: 0

Re: Apostrophes in assignment statements

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
Contributor n6
Contributor
Posts: 62

Re: Apostrophes in assignment statements

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Apostrophes in assignment statements

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.
Super Contributor
Posts: 359

Re: Apostrophes in assignment statements

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
Ask a Question
Discussion stats
  • 6 replies
  • 145 views
  • 0 likes
  • 5 in conversation