BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mehul4frnds
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want1;
  set have1;
  if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want1;
  set have1;
  if ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5));
run;
mehul4frnds
Obsidian | Level 7

Thanks for the reply. This works very well.

Is there any other way with using SQL also?

RW9
Diamond | Level 26 RW9
Diamond | Level 26
case when ttg < 4.5 then LDL_nw2 = TC - ((HDL)+(Ttg/5)) else . end asldl_nw2
mehul4frnds
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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