- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
We're doing Multiple Linear Regression in my statistics class. There's a lot of code snippets provided without context of what is happening and why. For this question on my homework, I'm assuming that we need to use dummy variables because the category is for Sex with the options being Male and Female as strings. I can't just put these into proc reg because it rejects the Sex variable as it is. I've been trying to create dummy variables with variations of the following code:
data file;
infile '\\Client\C$\Users\st\Desktop\Statistics\TETwithoutHeaders.txt'
delimiter='09'x;
input ID LOGACT21 Dosage Sex;
IF Sex = "Male" THEN sexAsNum = 0;
ELSE sexAsNum = 1;
proc print;
run;
I've also tried a provided code snippet from the professor in place of the if-else statement:
Do i=1 To _N_; If Sex="Male" then sexAsNum=1; else sexAsNum=0; end;
Instead of changing all of the "Male" rows to have a new column sexAsNum with the value of 1 and all "Female" rows to have the sexAsNum value of 0, the rows under the Sex column have had their values replaced with - and two new columns, i and sexAsNum, have been added. All of the rows of column i seem to have irrelevant incrementing and every sexAsNum value is 0 regardless of what Sex was previously.
Could someone please tell me what I'm doing wrong?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You read sex into a numeric variable in your input statement, so it cannot contain character values like "Male".
Start by correctly reading the file, and then decide how to handle the data.
If in doubt, post a few lines of your infile, using the {i} button.
@pstarr wrote:
We're doing Multiple Linear Regression in my statistics class. There's a lot of code snippets provided without context of what is happening and why. For this question on my homework, I'm assuming that we need to use dummy variables because the category is for Sex with the options being Male and Female as strings. I can't just put these into proc reg because it rejects the Sex variable as it is. I've been trying to create dummy variables with variations of the following code:
data file; infile '\\Client\C$\Users\st\Desktop\Statistics\TETwithoutHeaders.txt' delimiter='09'x; input ID LOGACT21 Dosage Sex; IF Sex = "Male" THEN sexAsNum = 0; ELSE sexAsNum = 1; proc print;
run;
I've also tried a provided code snippet from the professor in place of the if-else statement:
Do i=1 To _N_; If Sex="Male" then sexAsNum=1; else sexAsNum=0; end;
Instead of changing all of the "Male" rows to have a new column sexAsNum with the value of 1 and all "Female" rows to have the sexAsNum value of 0, the rows under the Sex column have had their values replaced with - and two new columns, i and sexAsNum, have been added. All of the rows of column i seem to have irrelevant incrementing and every sexAsNum value is 0 regardless of what Sex was previously.
Could someone please tell me what I'm doing wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You read sex into a numeric variable in your input statement, so it cannot contain character values like "Male".
Start by correctly reading the file, and then decide how to handle the data.
If in doubt, post a few lines of your infile, using the {i} button.
@pstarr wrote:
We're doing Multiple Linear Regression in my statistics class. There's a lot of code snippets provided without context of what is happening and why. For this question on my homework, I'm assuming that we need to use dummy variables because the category is for Sex with the options being Male and Female as strings. I can't just put these into proc reg because it rejects the Sex variable as it is. I've been trying to create dummy variables with variations of the following code:
data file; infile '\\Client\C$\Users\st\Desktop\Statistics\TETwithoutHeaders.txt' delimiter='09'x; input ID LOGACT21 Dosage Sex; IF Sex = "Male" THEN sexAsNum = 0; ELSE sexAsNum = 1; proc print;
run;
I've also tried a provided code snippet from the professor in place of the if-else statement:
Do i=1 To _N_; If Sex="Male" then sexAsNum=1; else sexAsNum=0; end;
Instead of changing all of the "Male" rows to have a new column sexAsNum with the value of 1 and all "Female" rows to have the sexAsNum value of 0, the rows under the Sex column have had their values replaced with - and two new columns, i and sexAsNum, have been added. All of the rows of column i seem to have irrelevant incrementing and every sexAsNum value is 0 regardless of what Sex was previously.
Could someone please tell me what I'm doing wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much, I removed the $ from Sex input during one of my earlier experiments with the code to accomplish something different and clearly missed putting it back. I've been so frustrated with this code that I completely overlooked it, thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There is absolutely no need to create dummy variables to do a regression. PROC GLM creates the dummy variables for you, so you don't have to.
Example:
proc glm data=file;
class sex;
model y = sex x1 x2 x3;
run;
quit;
Don't waste your time creating dummy variables for regression.
Paige Miller