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

I'm having trouble with the following symputx statement. I'm looking to end up with macros for each of the 4 chi square values.

 

%macro proc_code (i);

 

ods output chisq=chia&i;

proc freq data=collapsed_values;

tables discuss_pay*gender/chisq;

where discuss_pay ne . and gender=1 or gender=2;

run;

quit;

 

  data _null_;set chia&i;

       format tmp $40.;

       if Statistic="Chi-Square" then do;

         tmp=put(prob,6.4);

         if prob <.001 then do;

           tmp= "<.001";

         end;

         if prob <.0001 then do;

           tmp= "<.001";

         end;

         if prob gt .001 then do;

           tmp=trim(catt(round(prob,.001)));

         end;

       tmp=catt("Chi-Square Results CHISQ","(",DF,")=",round(value,.01)," p",tmp);

call symputx('chi&i',tmp);

end;

   RUN;   

%mend;

 

%proc_code (1);

%proc_code (2);

%proc_code (3);

%proc_code (4);

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Taking into account everything posted so far, two small changes are needed.  You have:

 

call symputx('chi&i',tmp);

 

You would need to replace this with:

 

call symputx("chi&i",tmp, 'G');

 

This takes care of two issues.  First, macro processing skips over any text in single quotes.  So to get &i to resolve, change the single quotes to double quotes.  And second, "G" will keep the macro variables around once the macro finishes executing.

View solution in original post

14 REPLIES 14
Reeza
Super User

I don't see how this will work. 

I think your WHERE condition is incorrect, and if you're always running the same proc with the same table statement the values won't differ regardless of your i value. 

 

 

ods output chisq=chia&i;

proc freq data=collapsed_values;

tables discuss_pay*gender/chisq;

where discuss_pay ne . and gender=1 or gender=2;

run;

quit;

 

I think this is what you want

 

where discuss_pay ne . and gender in (1,2);

 

You need to change your proc freq somehow I imagine?

bts
Calcite | Level 5 bts
Calcite | Level 5

The proc freq works (i removed the where statement to test it) - it's the macro that doesn't. I tried a %let statement in lieu of the symputx statement, but still no go. 

Reeza
Super User

It may work, but I don't think it's correct. 

 

 

bts
Calcite | Level 5 bts
Calcite | Level 5

oh i misunderstood - thank you very much for pointing that out. 

LinusH
Tourmaline | Level 20

I Assume that mean that you need to access those macro variables outside the macro?

It's about scope. Macro variable created within a macro exist only in local scope of the macro.

You need to define them as global.

Data never sleeps
bts
Calcite | Level 5 bts
Calcite | Level 5

No, I don't need to access it outside the macro. After running that code, I typed %put chi1, and i got the error: 

WARNING: Apparent symbolic reference CHI1 not resolved.

Astounding
PROC Star

Linus is right about that part.  Once the macro finishes, your macro variables disappear with it.  You could put this statement inside the macro instead:

 

%put &&chi&i;

 

But you will still find that &chi1 is equal to &chi2 is equal to &chi3 is equal to &chi4.  The output of PROC FREQ never changes.

Astounding
PROC Star

It looks like you plan on calling the macro 4 times.  So you don't need your macro to loop through 4 executions automatically.  Rather, focus on the PROC FREQ.  Each time PROC FREQ runs, it generates exactly the same output.  You might use ODS to place that output into a different data set each time, but the output of PROC FREQ doesn't change.

 

How do you want the results of PROC FREQ to change each time you call the macro?

bts
Calcite | Level 5 bts
Calcite | Level 5

I have another macro variable (&var) that cycles through a list of variables to be used in the proc freq. Although I used 4 in the example I posted, there are actually 60 variables, hence 60 chi square statistics that I want outputed chi1-chi60. So I thought I could output a macro variable that represents the 60 chi-squares. 

 

 

 

%macro proc_code(var,i);

 

ods output chisq=chi&i;

proc freq data=pd.griffin_collapsed_values;

tables &var*gender/chisq;

run;

quit;

 

data _null_;set chia&i;

       format tmp $40.;

       if Statistic="Chi-Square" then do;

         tmp=put(prob,6.4);

         if prob <.001 then do;

           tmp= "<.001";

         end;

         if prob <.0001 then do;

           tmp= "<.001";

         end;

         if prob gt .001 then do;

           tmp=trim(catt(round(prob,.001)));

         end;

       tmp=catt("Chi-Square Results CHISQ","(",DF,")=",round(value,.01)," p",tmp);

call symputx('chi&i',tmp);

end;

   RUN;    

 

%mend;

%proc_code( discuss_pay,1 );

%proc_code( doc_language,2);

%proc_code( undstd_docs,3 );

%proc_code( undstd_role,4);

bts
Calcite | Level 5 bts
Calcite | Level 5

When I add %put &&chi&i; within the macro, it returns a value of "tmp".

Reeza
Super User

Why a macro? Why not just run them all at once?

Whatever you're using the macro variables for later, it's probably easier if you have them all in a table.

 

ods output chisq=chi;
proc freq data=sashelp.cars;
tables (cylinders type origin)*make/chisq;
run;
quit;

data chi;
set chi;

variable = scan(table, 2, " *");
where Statistic="Chi-Square";
run;

proc print data=chi;
run;

 

bts
Calcite | Level 5 bts
Calcite | Level 5

Because after the chi-squares and before the macro ends, I'm creating graphs and will be putting their chi-square statistic in footnotes to each graph. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, I think there is a lot of unecessary code in there.  Why are you calling the macro 4 times to start with, you get the same output for each loop as the freq is exactly the same for each call?

 

ods output chisq=chia;
proc freq data=collapsed_values;
tables discuss_pay*gender/chisq;
where discuss_pay ne . and gender=1 or gender=2;
run;
data _null_;
length tmp $40;
set chiA;
if Statistic="Chi-Square" then do;
tmp=cat("Chi-Square Results CHISQ","(",DF,")=",round(value,.01)," p",ifc(prob <.001,"<.001",strip(round(prob,.001))));
call symputx('chi',tmp);
end;
run;

This is what your code resolves to to my mind, the only thing I can't work out is why the four calls - and why these can't be put into the proc freq?  Posting test data, in the form of a datastep, and what the output should look like would help. 

Astounding
PROC Star

Taking into account everything posted so far, two small changes are needed.  You have:

 

call symputx('chi&i',tmp);

 

You would need to replace this with:

 

call symputx("chi&i",tmp, 'G');

 

This takes care of two issues.  First, macro processing skips over any text in single quotes.  So to get &i to resolve, change the single quotes to double quotes.  And second, "G" will keep the macro variables around once the macro finishes executing.

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!

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
  • 14 replies
  • 2234 views
  • 2 likes
  • 5 in conversation