SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to run logistic regression model that I want to test.

Before doing that I want to create the following 2 tables:

table 1-

Show for each exploratory variable the following statistics:

Ronein_0-1724832769473.png

table 2-

show for each explanatory variable number of failures and no failures in each category.

 

Ronein_1-1724833042195.png

 

When I run this code I get the results but it looks bad and not organized in one table.

When I do copy from results-sas report  and paste it  in excel then it looks not nice

please note that the explanatory variables are all categorical variables 

 

ods html file="/usr/local/SAS/SASUsers/LabRet/UserDir/Q1234/freq_1.xls";
proc freq data=PANELCS_2211_Y_p_sample_7;
tables 
(
X
W
Q 
R
S 
T
) * Failure_FUTURE_Ind
 / nopercent nocol chisq;
run;
ods html close;

 

 

  

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
 set sashelp.heart;
run;


%macro chisq_test(dsn=,x=,y=);
ods select none;
proc freq data=&dsn;
tables &x*&y /chisq  ;
ods output  ChiSq = ChiSq ;
run;
data ChiSq;
 length var_name $ 40;
 set ChiSq;
 var_name="&y.";
 drop table;
run;
ods select all;
proc append base=want data=ChiSq force;run;
%mend;


proc delete data=want;run;
%chisq_test(dsn=have,x=status,y=sex)
%chisq_test(dsn=have,x=status,y=bp_status)
%chisq_test(dsn=have,x=status,y=smoking_status)

Ksharp_0-1724898976776.png

 

 

 

Here is an example for second picture.Maybe you could make it macroize to do efficiently.



proc sql;
select '    Sex' as Var_name length=40,sex    label='Category','Freq' as Measure length=40,
 put(sum(status='Dead'),20. -l) as Failure,put(sum(status='Alive'),20. -l) as NoFailure,put(count(*),20. -l) as TOTAL
 from have 
  group by sex
union all
select '    Sex' as Var_name,sex,'Row_PCT' as Measure,
 put(sum(status='Dead')/count(*),percent8.2 -l) as Failure,put(sum(status='Alive')/count(*),percent8.2 -l) as NoFailure,'100%' as TOTAL
 from have 
  group by sex





union all
select 'bp_status' as Var_name length=40,bp_status,'Freq' as Measure length=40,
 put(sum(status='Dead'),20. -l) as Failure,put(sum(status='Alive'),20. -l) as NoFailure,put(count(*),20. -l) as TOTAL
 from have 
  group by bp_status
union all
select 'bp_status' as Var_name,bp_status,'Row_PCT' as Measure,
 put(sum(status='Dead')/count(*),percent8.2 -l) as Failure,put(sum(status='Alive')/count(*),percent8.2 -l) as NoFailure,'100%' as TOTAL
 from have 
  group by bp_status




order by 1,2,3
;
quit;

Ksharp_2-1724899363219.png

 

 

View solution in original post

5 REPLIES 5
ballardw
Super User

Details.

You show single variables but several of the tests you show could be using two (or more) variables.

So do  you want single variable or x with w ?

Looks like Proc Freq and send the output to ODS OUTPUT to create data sets to merge if needed.

proc freq data=sashelp.class;
tables sex age /chisq  ;
ods output onewaychisq = work.mychisq;
run;

The output data set would need some manipulation, extract the variable name from the Table variable (scan works), the Label1 (or more) has values for the name of the statistic and a label. Use transpose to get your more desired output, or a report procedure.

You can use ODS TRACE to determine which objects are built by your code that you can select into data sets.

 

But this makes no sense:

show for each explanatory variable number of failures and no failures in each category.

 

ballardw_0-1724852888452.png

 

Define failure and no failure. Where is the data that would be used to determine such???? What are the rules?

Ronein
Meteorite | Level 14
You can see in my code that the tests are between each of independent variables x,w,q and so on and dependent variable Failure_FUTURE_Ind( indicator of failure)
ballardw
Super User

@Ronein wrote:
You can see in my code that the tests are between each of independent variables x,w,q and so on and dependent variable Failure_FUTURE_Ind( indicator of failure)

Does not tell me how to interpret results as failure or not. Or which data is used or how. I try not to guess at such.

Ronein
Meteorite | Level 14
This is credit risk world. For each independent variable for example level of wealth we check distribution of failures and no failures
Ksharp
Super User
data have;
 set sashelp.heart;
run;


%macro chisq_test(dsn=,x=,y=);
ods select none;
proc freq data=&dsn;
tables &x*&y /chisq  ;
ods output  ChiSq = ChiSq ;
run;
data ChiSq;
 length var_name $ 40;
 set ChiSq;
 var_name="&y.";
 drop table;
run;
ods select all;
proc append base=want data=ChiSq force;run;
%mend;


proc delete data=want;run;
%chisq_test(dsn=have,x=status,y=sex)
%chisq_test(dsn=have,x=status,y=bp_status)
%chisq_test(dsn=have,x=status,y=smoking_status)

Ksharp_0-1724898976776.png

 

 

 

Here is an example for second picture.Maybe you could make it macroize to do efficiently.



proc sql;
select '    Sex' as Var_name length=40,sex    label='Category','Freq' as Measure length=40,
 put(sum(status='Dead'),20. -l) as Failure,put(sum(status='Alive'),20. -l) as NoFailure,put(count(*),20. -l) as TOTAL
 from have 
  group by sex
union all
select '    Sex' as Var_name,sex,'Row_PCT' as Measure,
 put(sum(status='Dead')/count(*),percent8.2 -l) as Failure,put(sum(status='Alive')/count(*),percent8.2 -l) as NoFailure,'100%' as TOTAL
 from have 
  group by sex





union all
select 'bp_status' as Var_name length=40,bp_status,'Freq' as Measure length=40,
 put(sum(status='Dead'),20. -l) as Failure,put(sum(status='Alive'),20. -l) as NoFailure,put(count(*),20. -l) as TOTAL
 from have 
  group by bp_status
union all
select 'bp_status' as Var_name,bp_status,'Row_PCT' as Measure,
 put(sum(status='Dead')/count(*),percent8.2 -l) as Failure,put(sum(status='Alive')/count(*),percent8.2 -l) as NoFailure,'100%' as TOTAL
 from have 
  group by bp_status




order by 1,2,3
;
quit;

Ksharp_2-1724899363219.png

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 675 views
  • 1 like
  • 3 in conversation