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

Hello,

I was wanting to know how to make sure to change a continuous variable into a categorical variable for chi square analysis.

Would I use proc format or if then statements? I feel like if I use proc format I won't be able to do chi square statistics because it would make the numbers categorized into characters for a variable like age for example.

If then statements make a bit more sense renaming a new variable, in order to use in chi square analysis.

For example,

Data example1;

set example;

age=newage;

If age= 15-18 then newage= 1;

If age=19-27 then new age =2;

if age > 27 then new age=3 ;

run;

And new age would be the variable I would use in chi square analysis. Is this correct?

Any syntax missing from this code?

Thank you!!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Formats work. Your code seems to have an extra line whose purpose I don't seem to understand.

age=newage;

Seems to me to be extraneous.

You also need to use something like

if age>=15 and age<=18 then newage=1;

But to answer your question in a more broad sense, I know people have done this on occasion, but the idea of changing a continuous variable into a category just so you can perform a chi-squared test is inefficient. Better you should use all the information you have in your analysis, which requires you to treat age as a continuous variable which should provide a more powerful, and then the chi-squared test is inappropriate.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Formats work. Your code seems to have an extra line whose purpose I don't seem to understand.

age=newage;

Seems to me to be extraneous.

You also need to use something like

if age>=15 and age<=18 then newage=1;

But to answer your question in a more broad sense, I know people have done this on occasion, but the idea of changing a continuous variable into a category just so you can perform a chi-squared test is inefficient. Better you should use all the information you have in your analysis, which requires you to treat age as a continuous variable which should provide a more powerful, and then the chi-squared test is inappropriate.

--
Paige Miller
ballardw
Super User

To expand on Paige's comments:

Create a custom format such as:

proc format;

value myagegroup

15 - 18 = '15 to 18 years'

19 - 27 = '19 to 27 years'

28 - high = '28 and older'

;

run;

Then in the code you are running the analysis on you don't need to create a new variable, use AGE and add as statement:

format age myagegroup. ;

This will create the groups to be used by the chi-square AND get useable value labels.

Note: if you have ages less than 15 then each age would appear as a separate group.

One nice thing about this approach is when someone asks "what happens if we use 15-23, 24-28 and then larger you only need to create a new format, not another variable. Also if you create output datasets such as with proc freq you will get one value, usually the lowest value that actually appears in the group for the formatted variable in the output.

Macyhe22
Calcite | Level 5

Thank you both for your assistance

Macyhe22
Calcite | Level 5

Thanks so much for the detailed information. I appreciate it greatly!

Macyhe22
Calcite | Level 5

Thank you! I didn't know you could reduce the strength of the analysis by categorizing variables. I will look into other bivariate analysis tests. Thank you

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 22111 views
  • 4 likes
  • 3 in conversation