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

Hi,

iam trying below code but iam getting amt column values as '0.00'.but i need the amt value to be -229 and 229 based on below conditions .

please help where iam going wrong

case when Sign='D'then Amt = (-1 * Amt ) else Amt
else when Sign='C' then Amt =(1 * Amt ) else Amt
For example if amt column holds 299 the if it is D then -299 and if it is C then amt should be 299

Amt

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Loose the amt= part in your case statements and the last comma like this

 

proc sql;
create view work.W4J4UL3 as
select

(case when sign ='C' then -1 * Amt
else Amt
end) as Amt length = 8
format = 11.2
informat = 11.2,
(case when Sign1='C'
then (-1 * Amt1 )
else Amt1
end) as Amt1 length = 8
format = 11.2
informat = 11.2

from xxxx
;
quit;

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

can you please post your entire PROC SQL Step?

JJP1
Pyrite | Level 9

Hi @PeterClemmensen ,please find below and kindly help


proc sql;
create view work.W4J4UL3 as
select

(case when sign ='C' then Amt = -1 * Amt
else Amt
end) as Amt length = 8
format = 11.2
informat = 11.2,
(case when Sign1='C'
then Amt1=(-1 * Amt1 )
else Amt1
end) as Amt1 length = 8
format = 11.2
informat = 11.2,

from xxxx
;
quit;

Kurt_Bremser
Super User

Start with most simple version, and DO NOT write spaghetti code:

data have;
input sign $ amt;
datalines;
D 299
C 299
;

proc sql;
create table want as
select
  sign,
  case
    when sign = 'C' then -amt
    else amt
  end as amt
from have;
quit;
JJP1
Pyrite | Level 9

Thanks @Kurt_Bremser .It is working as expected.i follow as you suggested,thanks

PeterClemmensen
Tourmaline | Level 20

Loose the amt= part in your case statements and the last comma like this

 

proc sql;
create view work.W4J4UL3 as
select

(case when sign ='C' then -1 * Amt
else Amt
end) as Amt length = 8
format = 11.2
informat = 11.2,
(case when Sign1='C'
then (-1 * Amt1 )
else Amt1
end) as Amt1 length = 8
format = 11.2
informat = 11.2

from xxxx
;
quit;
JJP1
Pyrite | Level 9

Thanks @PeterClemmensen and @Kurt_Bremser & @PaigeMiller .

i am getting what i expected in output.Thanks for quickly helping me on this

PaigeMiller
Diamond | Level 26

The syntax for SQL (I assume this is what you are using, please confirm) does not use the equal sign after THEN

 

So you want

 

case when Sign='D' then -Amt
when Sign='C' then Amt end as Amt

 

--
Paige Miller
JJP1
Pyrite | Level 9

Thanks @PaigeMiller .Actually i should do soemthing like (-1 * Amt).you mean this is same as -Amt please.and can we not apply -1*amt ? please

PaigeMiller
Diamond | Level 26

These should produce the same results.

--
Paige Miller
JJP1
Pyrite | Level 9

Iam getting 0.00 only please,please have a look on test code i posted on reply to @PeterClemmensen  please

PaigeMiller
Diamond | Level 26

@JJP1 wrote:

Iam getting 0.00 only please,please have a look on test code i posted on reply to @PeterClemmensen  please


As I stated, and as I and others have shown by example, you do not want an equal sign after THEN. The presence of the equal sign after THEN means you are testing to see if two things are equal in the THEN clause, which is NOT what you want. (You do want to test things are equal in the WHEN clause, not in the THEN clause)

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 11 replies
  • 581 views
  • 1 like
  • 4 in conversation