SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to change a continuous variable to a categorical variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How to change a continuous variable to a categorical variable?

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!!


Accepted Solutions
Solution
‎04-02-2015 12:54 PM
Trusted Advisor
Posts: 1,931

Re: How to change a continuous variable to a categorical variable?

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.

View solution in original post


All Replies
Solution
‎04-02-2015 12:54 PM
Trusted Advisor
Posts: 1,931

Re: How to change a continuous variable to a categorical variable?

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.

Super User
Posts: 11,343

Re: How to change a continuous variable to a categorical variable?

Posted in reply to PaigeMiller

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.

Occasional Contributor
Posts: 13

Re: How to change a continuous variable to a categorical variable?

Thank you both for your assistance

Occasional Contributor
Posts: 13

Re: How to change a continuous variable to a categorical variable?

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

Occasional Contributor
Posts: 13

Re: How to change a continuous variable to a categorical variable?

Posted in reply to PaigeMiller

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

🔒 This topic is solved and locked.

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

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