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

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 -

 

TableName1Label1cValue1nValue1num_varuchar_varz
Variable FLG_PCHI_Chi-Square1246.5221246.522121246.52211246.522
Variable FLGDF_PCHIDF111.00E-301
Variable FLGP_PCHIPr > ChiSq<.00014.73E-273.<.0001

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

8 REPLIES 8
dwights
Fluorite | Level 6

Thanks for replying. I tried both of them. None worked.

Reeza
Super User

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

 

 


 

dwights
Fluorite | Level 6
It prints the following -

char_varzxy = <.0001
Tom
Super User Tom
Super User

@Reeza That test will not reveal leading/trailing spaces.

Use something like this instead:

%PUT |&char_varzxy|;

 

Tom
Super User Tom
Super User

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.

dwights
Fluorite | Level 6

Thank you very much! The trimmed keyword and the double quotes helped me.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to Concatenate Values

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.

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
  • 8 replies
  • 4309 views
  • 10 likes
  • 4 in conversation