I'm trying to check the value of the character macro variable 'char_varzxy ' in a %if condition. The if loop is not being executed even if the condition holds true. Can someone please let me know why
%do i=1 %to &N;
/* Code to get the table containing 'FLG' variable on which chi-square test is being run.
*
*
*
*
*/
ods exclude all;
proc freq data=temp2 ORDER=formatted;
tables &variable / TestP=(&P_Values);
ods output OneWayFreqs=FreqOut OneWayChiSq=ChiSData;
output out=FreqStats N ChiSq;
run;
ods exclude none;
data ChiSqData2;
set ChiSData;
num_varu = input(cValue1,32.30);
char_varz= put(cValue1,12.8);
run;
proc sql noprint;
select nValue1 into :nValue1 from ChiSqData2 where Label1='Pr > ChiSq';
quit;
proc sql noprint;
select num_varu into :num_vary from ChiSqData2 where Label1='Pr > ChiSq';
quit;
proc sql noprint;
select cValue1 into :prob_vary from ChiSqData2 where Label1='Pr > ChiSq';
quit;
proc sql noprint;
select char_varz into :char_varzxy from ChiSqData2 where Label1='Pr > ChiSq';
quit;
%if(&char_varzxy ='<.0001') %then %do;
/* Not entering this loop */
%end;
%end;
The chisqdata2 table which contains the character variable is as follows -
Table | Name1 | Label1 | cValue1 | nValue1 | num_varu | char_varz |
Variable FLG | _PCHI_ | Chi-Square | 1246.522 | 1246.52212 | 1246.5221 | 1246.522 |
Variable FLG | DF_PCHI | DF | 1 | 1 | 1.00E-30 | 1 |
Variable FLG | P_PCHI | Pr > ChiSq | <.0001 | 4.73E-273 | . | <.0001 |
I don't see any code where you are putting single quotes into the value of the macro variable. Unless that is what the ODS output does, but I doubt that.
You do appear to be putting trailing spaces into the value of the macro variable however.
select char_varz into :char_varzxy from ChiSqData2 where Label1='Pr > ChiSq';
Either remove the trailing spaces.
%let char_varzxy=&char_varzxy;
Or don't put them into the value to begin with by adding the TRIMMED keyword to the INTO clause.
select char_varz
into :char_varzxy trimmed
from ChiSqData2
where Label1='Pr > ChiSq'
;
Because your macro variable contains < you should use quoting to avoid confusing SAS about what test you are doing. It is probably easier to use actual quotes instead of macro quoting.
%if "<.0001" = "&char_varzxy" %then ....
Make sure to use double quote character and not single quote character because macro variable references are not resolved inside of single quotes. Make sure both sides use the same quote character. A single quote character is not equal to a double quote character.
Since the macro processor works with text only, quotes around strings are not needed.
%if &char_varzxy = <.0001 %then %do;
or
%if "&char_varzxy" = "<.0001" %then %do;
Thanks for replying. I tried both of them. None worked.
If you do the following, what shows up?
%PUT &char_varzxy;
The variable may be numeric and the <0.001 is a formatted display so the value saved may be the exact value rather than the formatted value.
@dwights wrote:
I'm trying to check the value of the character macro variable 'char_varzxy ' in a %if condition. The if loop is not being executed even if the condition holds true. Can someone please let me know why
%do i=1 %to &N; /* Code to get the table containing 'FLG' variable on which chi-square test is being run. * * * * */ ods exclude all; proc freq data=temp2 ORDER=formatted; tables &variable / TestP=(&P_Values); ods output OneWayFreqs=FreqOut OneWayChiSq=ChiSData; output out=FreqStats N ChiSq; run; ods exclude none; data ChiSqData2; set ChiSData; num_varu = input(cValue1,32.30); char_varz= put(cValue1,12.8); run; proc sql noprint; select nValue1 into :nValue1 from ChiSqData2 where Label1='Pr > ChiSq'; quit; proc sql noprint; select num_varu into :num_vary from ChiSqData2 where Label1='Pr > ChiSq'; quit; proc sql noprint; select cValue1 into :prob_vary from ChiSqData2 where Label1='Pr > ChiSq'; quit; proc sql noprint; select char_varz into :char_varzxy from ChiSqData2 where Label1='Pr > ChiSq'; quit; %if(&char_varzxy ='<.0001') %then %do; /* Not entering this loop */ %end; %end;
The chisqdata2 table which contains the character variable is as follows -
Table Name1 Label1 cValue1 nValue1 num_varu char_varz Variable FLG _PCHI_ Chi-Square 1246.522 1246.52212 1246.5221 1246.522 Variable FLG DF_PCHI DF 1 1 1.00E-30 1 Variable FLG P_PCHI Pr > ChiSq <.0001 4.73E-273 . <.0001
It prints the following -
char_varzxy = <.0001
@dwights wrote:
It prints the following -
char_varzxy = <.0001
See? No quotes.
@Reeza That test will not reveal leading/trailing spaces.
Use something like this instead:
%PUT |&char_varzxy|;
I don't see any code where you are putting single quotes into the value of the macro variable. Unless that is what the ODS output does, but I doubt that.
You do appear to be putting trailing spaces into the value of the macro variable however.
select char_varz into :char_varzxy from ChiSqData2 where Label1='Pr > ChiSq';
Either remove the trailing spaces.
%let char_varzxy=&char_varzxy;
Or don't put them into the value to begin with by adding the TRIMMED keyword to the INTO clause.
select char_varz
into :char_varzxy trimmed
from ChiSqData2
where Label1='Pr > ChiSq'
;
Because your macro variable contains < you should use quoting to avoid confusing SAS about what test you are doing. It is probably easier to use actual quotes instead of macro quoting.
%if "<.0001" = "&char_varzxy" %then ....
Make sure to use double quote character and not single quote character because macro variable references are not resolved inside of single quotes. Make sure both sides use the same quote character. A single quote character is not equal to a double quote character.
Thank you very much! The trimmed keyword and the double quotes helped me.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.