## How to calculate new variable with arithmetic formula and if then command

Solved
Occasional Contributor
Posts: 16

# How to calculate new variable with arithmetic formula and if then command

Hello to all,

I am trying to calculate a new variable with the arithmetic equation.  Here I want  a new variable (LDL_new) should be calculated only if as Ttg is less than 4.5

My data looks like this:

data have1;
input id name\$ gen\$ PROCHI DATE TC HDL LDL Ttg;
datalines;
1 ram male 1 1.1 8.0 3.0 1.45 1
2 mohan male 1 1.1 8.5 2.9 1.24 1
3 shyam male 2 2.1 6.8 3.1 2.23 2
4 radha female 2 2.1 7.2 3.8 3.80 3
5 gita female 2 2.1 . 2.7 3.45 3
6 sita female 2 3.1 8.9 6.8 4.56 2
7 ranu female 3 2.1 7.5 5.9 1.83 5
8 tina female 3 2.1 3.6 2.8 1.23 8
9 shan male 4 8.1 2.8 1.1 1.67 9
;
run;

I have used this command to calculate new variable LDL_new

data want1;
set have1;
LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;

I want data like this:

SAS Output                      id        name       gen        PROCHI DATE      TC       HDL     LDL      Ttg       LDL_nw

 1 ram male 1 1.1 8.0 3 1.45 1 4.8 2 mohan male 1 1.1 8.5 2.9 1.24 1 5.4 3 shyam male 2 2.1 6.8 3.1 2.23 2 3.3 4 radha female 2 2.1 7.2 3.8 3.8 3 2.8 5 gita female 2 2.1 . 2.7 3.45 3 . 6 sita female 2 3.1 8.9 6.8 4.56 2 1.7 7 ranu female 3 2.1 7.5 5.9 1.83 5 8 tina female 3 2.1 3.6 2.8 1.23 8 9 shan male 4 8.1 2.8 1.1 1.67 9

Kindly suggest.

Accepted Solutions
Solution
‎04-19-2018 10:58 AM
Super User
Posts: 9,227

## Re: How to calculate new variable with arithmetic formula and if then command

```data want1;
set have1;
if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;```

All Replies
Solution
‎04-19-2018 10:58 AM
Super User
Posts: 9,227

## Re: How to calculate new variable with arithmetic formula and if then command

```data want1;
set have1;
if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;```
Occasional Contributor
Posts: 16

## Re: How to calculate new variable with arithmetic formula and if then command

Thanks for the reply. This works very well.

Is there any other way with using SQL also?

Super User
Posts: 9,227

## Re: How to calculate new variable with arithmetic formula and if then command

`case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5)) else . end asldl_nw2`
Occasional Contributor
Posts: 16

## Re: How to calculate new variable with arithmetic formula and if then command

I tried this command

proc sql ;
create table want1
case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5))
else .
from have1
end as ldl_nw2 ;
quit;

it says:

15 proc sql ;
16 create table want1 as
17 case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5))
---- ----
22 76
202
ERROR 22-322: Syntax error, expecting one of the following: (, SELECT.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

17 ! case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5))
----
22
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &, *, **, +,
',', -, /, <, <=, <>, =, >, >=, AND, AS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT,
INFORMAT, INTO, LABEL, LE, LEN, LENGTH, LET, LT, LTT, NE, NET, NOT, OR, TRANSCODE,
^, ^=, |, ||, ~, ~=.

18 else .
19 from have1
20 end as ldl_nw2 ;
21 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.06 seconds
cpu time 0.01 seconds

Kindly suggest

Super User
Posts: 9,227

## Re: How to calculate new variable with arithmetic formula and if then command

Sorry, I assumed you knew how to program using proc sql.  Perhaps read on up that?  You are missing a select and form is in the wrong place.

```proc sql ;
create table want1 as
select case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5)) else . end as ldl_nw2
from have1;
quit;```
☑ This topic is solved.