BookmarkSubscribeRSS Feed

Hi,

Can anyone help re write my case statement please so it works?

proc sql;

create table adjusted_movement as

select *,

case when ltd_allocations gt runnning_sum then 'current' else if ltd_Allocations + weekly_movement gt dealt_amount

then running_sum - ltd_allocations else weekly_movement end as adjusted_movement

from

Allocate_rates_LTD_Allocations;

quit;

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

proc sql;

  create table adjusted_movement as

  select   *,

                case   when ltd_allocations gt runnning_sum then 'current'

                            when ltd_Allocations + weekly_movement gt dealt_amount then running_sum - ltd_allocations

                            else weekly_movement end as adjusted_movement

  from      Allocate_rates_LTD_Allocations;

quit;

I now get this error message?

540  proc sql;

541  create table adjusted_movement as

542  select*,

543  case

544  when ltd_allocations gt running_sum then 'current'

545  when ltd_allocations + weekly_movement gt dealt_amount_ then running_sum - ltd_allocations

546  else weekly_movement end as adjusted_movement

547  from

548  Allocate_rates_LTD_Allocations;

ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.

549  quit;

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE SQL used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, thats because the logic of the second when results in a numeric value, the first 'current'.  You need to decide what you want the outcome to be, for instance, if the column is to be text then:

proc sql;

  create table adjusted_movement as

  select   *,

                case   when ltd_allocations gt runnning_sum then 'current'

                            when ltd_Allocations + weekly_movement gt dealt_amount then put(running_sum - ltd_allocations,best.)

                            else put(weekly_movement,best.) end as adjusted_movement

  from      Allocate_rates_LTD_Allocations;

quit;

If its to be numeric then you can't have current, maybe:

proc sql;

  create table adjusted_movement as

  select   *,

                case   when ltd_allocations gt runnning_sum then 99999

                            when ltd_Allocations + weekly_movement gt dealt_amount then running_sum - ltd_allocations

                            else weekly_movement end as adjusted_movement

  from      Allocate_rates_LTD_Allocations;

quit;

Kurt_Bremser
Super User

Try:

proc sql;

create table adjusted_movement as

select *,

case when ltd_allocations gt runnning_sum then 'current' when ltd_Allocations + weekly_movement gt dealt_amount

then running_sum - ltd_allocations else weekly_movement end as adjusted_movement

from

Allocate_rates_LTD_Allocations;

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
  • 4 replies
  • 614 views
  • 0 likes
  • 3 in conversation