Proc Freq

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

Proc Freq


Hi

I have 10 variables in the same dataset.

I have to do a contingency tables between a variable and each of those 10 variables seperately. So I have 10 tables now

and have to get the results for all the 10 in the same PDF

Could someone help me with that

Regards


Accepted Solutions
Solution
‎10-16-2012 11:30 AM
Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

%macro dothis;

%let varnames=wnd_dhl wnd_hmto wnd_seroma wnd_necrosis;

ods pdf file='yourfile.pdf";

%do ii=1 %to %sysfunc(countw(&varnames));

     %let thisvar=%scan(&varnames,&ii,%str( ));

     data _null_;

         set X(obs=1);

         call symputx('thislabel',vlabel(&thisvar));

     run;

     title "Surgery Time for Operations versus &thislabel";

     proc freq data=x;

          tables Dr*&thisvar/nopercent fisher  relrisk;

          exact pchi or;

     run;

%end;

ods pdf close;

%mend;

%dothis

View solution in original post


All Replies
Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

ods pdf file="yourfile.pdf";

proc freq;

...

run;

ods pdf close;

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Hi ,

Thanks for the reply.

For each of the 10 times I do it the var1 changes 10 times

and so do the footnotes and titles change for all of the 10 steps.

Can you help me put it in a macro and still be able to get all of the results in the same PDF?????

Is a macro required?can these 10 steps be reduced?

ods pdf file="yourfile.pdf";

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var1/nopercent fisher  relrisk;

exact pchi or;

title "aaaaaaaaaaaaaaaaaaaaaaaa";

footnote1 "bbbbbbbbbbbbbbbbbbbb";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var2/nopercent fisher  relrisk;

exact pchi or;

title "cccccccccccccccccccccc";

footnote1 "dddddddddddddddddddddd";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var3/nopercent fisher  relrisk;

exact pchi or;

title "eeeeeeeeeeeeeeeeeeeeeeeeee";

footnote1 "fffffffffffffffffffffffffffffffffffffffff";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var4/nopercent fisher  relrisk;

exact pchi or;

title "gggggggggggggggggggggggggggg";

footnote1 "hhhhhhhhhhhhhhhhhhhhhhhh";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var5/nopercent fisher  relrisk;

exact pchi or;

title "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";

footnote1 "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var6/nopercent fisher  relrisk;

exact pchi or;

title "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk";

footnote1 "llllllllllllllllllllllllllllllllllllllllllllllllllll";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var7/nopercent fisher  relrisk;

exact pchi or;

title "mmmmmmmmmmmmmmmmmmmmm";

footnote1 "nnnnnnnnnnnnnnnnnnnnnnn";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var8/nopercent fisher  relrisk;

exact pchi or;

title "oooooooooooooooooooooooooo";

footnote1 "pppppppppppppppppppppppp";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var9/nopercent fisher  relrisk;

exact pchi or;

title "qqqqqqqqqqqqqqqqqqqqq";

footnote1 "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrr";

run;

proc freq data=X(Rename=(Dr=Duration));

tables Duration*var10/nopercent fisher  relrisk;

exact pchi or;

title "ssssssssssssssssssssssssssssssss";

footnote1 "tttttttttttttttttttttttttttttttttttttttttttttttt";

run;

ods pdf close;

Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

If the footnotes and titles change in every step, then I think your code is the way to go. I don't see how a macro would help here, unless there's some structure to the titles and footnotes that you haven't told us.

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Hi,

The footnote can be omitted becaus it just says I have clubbed N/a with N

Secondly, The titles everytime go like:

Duration Vs var1

Duration  vs var2

....

Duration Vs var10

Thanks

Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

karun wrote:

Hi,

The footnote can be omitted becaus it just says I have clubbed N/a with N

Secondly, The titles everytime go like:

Duration Vs var1

Duration  vs var2

....

Duration Vs var10

Thanks

Is Var1 actually the text Var1?

Or is it the variable name? (If so, please let us know the 10 variable names)

Or is it the variable label?

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Hi,

It goes like this:

Surgery time for Operation Versus the label of the 10 variables actually ....(each time)

Thanks

Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

%macro dothis;

ods pdf file='yourfile.pdf";

%do ii=1 %to 10;

     data _null_;

         set X(obs=1);

         call symputx('thislabel',vlabel(var&ii));

     run;

     title "Surgery Time for Operations versus &thislabel";

     proc freq data=x;

          tables Dr*var&ii/nopercent fisher  relrisk;

          exact pchi or;

     run;

%end;

ods pdf close;

%mend;

%dothis

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Hi

Thanks a lot for the effort.

Its pleasing but I dint understand some of the things as i am a beginner

1)where are the variables?we just said ii=1 %to 10;

But how will it know what are those variables??????

what is setX(obs=1) doing???

Could you explain this program a little bit if you dont mind???

Thanks

Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

In the line tables dr*var&ii : the macro replaces the text string "var&ii" with var1 when ii is 1, and with var 2 when ii is 2, etc.

The data _null_ step is there simply the extract the label text for each variable, and place the label text into the macro variable &thislabel

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Sorry sorry sorry

My Bad..

The variables are not var1 to var10.

That was an example that 10 different variables exists

variable names are like wnd_dhl   ,   wnd_hmto,  wnd_seroma , wnd_necrosis  etccccc

Instead of typing them individually i just did var1-var10

Solution
‎10-16-2012 11:30 AM
Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

%macro dothis;

%let varnames=wnd_dhl wnd_hmto wnd_seroma wnd_necrosis;

ods pdf file='yourfile.pdf";

%do ii=1 %to %sysfunc(countw(&varnames));

     %let thisvar=%scan(&varnames,&ii,%str( ));

     data _null_;

         set X(obs=1);

         call symputx('thislabel',vlabel(&thisvar));

     run;

     title "Surgery Time for Operations versus &thislabel";

     proc freq data=x;

          tables Dr*&thisvar/nopercent fisher  relrisk;

          exact pchi or;

     run;

%end;

ods pdf close;

%mend;

%dothis

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Hi,

It works really really nice. Sorry for the trouble.

Also I want to rename the  Dr variable to Surgery Duration. How and where can I do that in the code???

Secondly, could you explain these 3 lines in the code???

%do ii=1 %to %sysfunc(countw(&varnames));

     %let thisvar=%scan(&varnames,&ii,%str( ));

set X(obs=1);     <<<<<----why obs=1????

Thanks

Trusted Advisor
Posts: 1,932

Re: Proc Freq

Posted in reply to robertrao

I want to rename the  Dr variable to Surgery Duration. How and where can I do that in the code???

Probably it is a good idea to use a label statement in PROC FREQ. Either that or change the name in data set X before you run the macro.

%do ii=1 %to %sysfunc(countw(&varnames));

     %let thisvar=%scan(&varnames,&ii,%str( ));

Loop over all of the variable names in &varnames; Each time through the loop, extract i-th variable name from &varnames and call this &thisvar

set X(obs=1); 
<<<<<----why obs=1????

To extract variable labels, you don't need the entire data set; SAS can do this with just one observation, as the label must be the same for the other observations

Super Contributor
Posts: 1,041

Re: Proc Freq

Posted in reply to PaigeMiller

Thanks a ton and I appreciate your time.

Regards

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 22 replies
  • 530 views
  • 7 likes
  • 3 in conversation