Adding variables

Accepted Solution Solved
Reply
Occasional Contributor H_G
Occasional Contributor
Posts: 18
Accepted Solution

Adding variables

Hello,

I have two questions I will be grateful if you can help me.

1-I would like to create new variable by adding some variables,for example:

newvar=0.85753* v1 +0.73453* v2 -0.10386* v3 -0.15108* v4 +0.20198* v5  -0.1217* v6  +0.25652* 7;

but because these variables have missing values I can not add them by "+", also I do not know why I could not use Sum (of ..) command in this case, maybe because I am multiplying numbers to my variables!

would you please help me how can I do it?

2- I am running a logistic regression, do you know how can I check for robustness of my result in SAS?

Thanks a lot.


Accepted Solutions
Solution
‎02-27-2013 02:10 AM
Contributor
Posts: 42

Re: Adding variables

Hello,

if you do not want to have missing values as a result, you need to decide which values you want to have to replace them. I think the most straightforward solution is to write coalesce function around your variables in your formula.

For example

newvar =0.85 * coalesce(v1,0)+0.73 *coalesce( v2,0)-0.1 * coalesce(v3,0) -0.15 * coalesce(v4,0)+ 0.2 * coalesce(v5,0) -0.12 * coalesce(v6,0)+0.26 * coalesce(v7,0);

if you want other values than zeros for missing cases (of v1, v2 ...) just write them down as the second parametr instead of zero.

Jakub

View solution in original post


All Replies
Super Contributor
Posts: 543

Re: Adding variables

Hi,

I may be completely out of line here, but I think for question # 1 you could use a piece of code like this:

(I am using sashelp.class data set, and want to predict weight < 120 using simple logistic with age and height as my covariates)

data in;

    set sashelp.class;

    w_lt120 = (weight < 120);

    for_merge = 1;*create a variable I will use later for merging;

    if age = 13 then age = .;*force some data to missing;

run;

proc logistic data = in;

    model w_lt120 = age height;

    ods output ParameterEstimates = estim;

run;

proc transpose data = estim out = t_estim prefix = est_; id  variable;var estimate;run;

data t_estim;set t_estim; for_merge = 1;run;

*bring it back;

data want;

    merge in t_estim;

    by for_merge;

    p_hat = est_intercept + est_age*age + est_height* height;

     *this will save you the time of typing the estimates by hand;

     *I think;

run;

For #2 not sure, but I am sure there are plenty of goodness of fit tests you cna use...

Smiley Happy

Occasional Contributor H_G
Occasional Contributor
Posts: 18

Re: Adding variables

Posted in reply to AncaTilea

Thank you so much for helping me , but unfortunately I could not understand the solution.

let me explain more my first question,

I want to create new variable, "newvar" which is the sum of some of some portion of existing variables v1, v2,...

so normally I use sum( of ) command to avoid missing values, but in this case as I am using portion of my variables, I can not use sum (of) command I think, and because of missing value I can not use "+" too.

I will appreciate if someone can help me,

it seems simple but I am stocking there for few hours.

Thanks

Super Contributor
Posts: 543

Re: Adding variables

could you try something like this: If v1 ne . & v2 ne . & v3 ne . & v4 ne . & v5 ne . & v6 ne . Then newvar ....your code;

Super Contributor
Posts: 1,636

Re: Adding variables

Hi,

example:

data have;

input a b c d;

cards;

2 3 4 .

5 6 . 2

;

data want;

  set have;

  newvar=sum(a,b*2,0.5*c,0.88*d);

proc print;run;

Super User
Posts: 5,513

Re: Adding variables

H.G.,

For the summation part, here's the idea (taking liberties with the actual formula):

newvar = sum(0.85 * v1, 0.73 * v2, -0.1 * v3, -0.15 * v4, 0.2 * v5, -0.12 * v6, 0.26 * v7);

No need for "of".

Good luck.

Occasional Contributor H_G
Occasional Contributor
Posts: 18

Re: Adding variables

Posted in reply to Astounding

hello, thank you so much for your answer, I tried what you told me, but still when I print the new variable is all missing value.

I know that I have missing value for some existing variables, but as long as I know when I use this sum command instead of"+" it should assume all the missing values as zero.

I will be grateful if you can please let me know if you know the problem

Thank you.

Super Contributor
Posts: 1,636

Re: Adding variables

do you want your a zero if all you variables are missing?

data have;

input a b c d;

cards;

2 3 4 .

5 6 . .

. . . .

;

data want;

  set have;

  newvar=sum(a,b*2,0.5*c,0.88*d,0);

proc print;run;

                                Obs    a    b    c    d    newvar

                                 1     2    3    4    .      10

                                 2     5    6    .    .      17

                                 3     .    .    .    .        0

Super Contributor
Posts: 1,636

Re: Adding variables

or

options missing=0;

data have;

input a b c d;

cards;

2 3 4 .

5 6 . .

. . . .

;

data want;

  set have;

  newvar=sum(a,b*2,0.5*c,0.88*d,0);

proc print;run;

                                 Obs   a    b    c    d    newvar

                                 1     2    3    4    0      10

                                 2     5    6    0    0      17

                                 3     0    0    0    0       0

Solution
‎02-27-2013 02:10 AM
Contributor
Posts: 42

Re: Adding variables

Hello,

if you do not want to have missing values as a result, you need to decide which values you want to have to replace them. I think the most straightforward solution is to write coalesce function around your variables in your formula.

For example

newvar =0.85 * coalesce(v1,0)+0.73 *coalesce( v2,0)-0.1 * coalesce(v3,0) -0.15 * coalesce(v4,0)+ 0.2 * coalesce(v5,0) -0.12 * coalesce(v6,0)+0.26 * coalesce(v7,0);

if you want other values than zeros for missing cases (of v1, v2 ...) just write them down as the second parametr instead of zero.

Jakub

Occasional Contributor H_G
Occasional Contributor
Posts: 18

Re: Adding variables

Thank you all for your answers and helping me,

Best Regards.

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 346 views
  • 6 likes
  • 5 in conversation