DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

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

1rammale11.18.03.01.4514.8
2mohanmale11.18.52.91.2415.4
3shyammale22.16.83.12.2323.3
4radhafemale22.17.23.83.8032.8
5gitafemale22.1.2.73.453.
6sitafemale23.18.96.84.5621.7
7ranufemale32.17.55.91.835 
8tinafemale32.13.62.81.238 
9shanmale48.12.81.11.679 

 

 

Kindly suggest.

Thanks in advance

 

 


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

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

Posted in reply to mehul4frnds
data want1;
  set have1;
  if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;

View solution in original post


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

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

Posted in reply to mehul4frnds
data want1;
  set have1;
  if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;
Contributor
Posts: 21

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
Super User
Posts: 9,866

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

Posted in reply to mehul4frnds
case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5)) else . end asldl_nw2
Contributor
Posts: 21

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
Super User
Posts: 9,866

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

Posted in reply to mehul4frnds

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.

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

Discussion stats
  • 5 replies
  • 141 views
  • 2 likes
  • 2 in conversation