SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pstarr
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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?


 

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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?


 

pstarr
Calcite | Level 5

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.

PaigeMiller
Diamond | Level 26

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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 1362 views
  • 1 like
  • 3 in conversation