Home > Enterprise >  SQL how to add a column to case when
SQL how to add a column to case when

Time:01-28

I have the sql code, where in the first part I create the credits table, where I have the kl_nazwa items. Now I want to create another table and add a country to each item, but I would like to assign values from kl_nazwa to the item credits. But my code isn't work . How is should do it ?

My code:

proc sql;

create table COREP as
select 
    spr_DATA_SPR           as DATA_DANYCH      format yymmdd10.,
    "Kredyty hipoteczne"   as RODZAJ_AKTYWÓW   format $50. length=50,
    ID                     as ID               format $50. length=50,
    kl_NAZWA               
    spr_WAL_OPIS           as WALUTA           format $3.  length=3,
    CRD_EKSP_PIER_DC_FIN   as EXP_PIERWOTNA    format commax16.2,
    -CRD_KOR_DC_FIN        as KOREKTA          format commax16.2,
    CRD_AC_ORIGIN          as CRD_AC_ORIGIN    format $2.  length=2,
    CRD_AC                 as CRD_AC           format $2.  length=2,
    CRD_CCF                as CRD_CCF          format commax16.2,
    CRD_RWG                as CRD_RWG          format commax16.2,
    DESC_2                 as CRD_BIL_POZABIL  format $1.
from kredyty;

proc sql;

create table COREP as
select (*)
  case 
        when (RODZAJ_AKTYWÓW) = 'Instrumenty pochodne' Then 'Polska'
        when (RODZAJ_AKTYWÓW) = 'Pozostałe aktywa'    then 'Polska'
        when (RODZAJ_AKTYWÓW) = 'Nostra' then 'Polska' 
        when (RODZAJ_AKTYWÓW) = 'Papiery wartościowe'    then 'Polska'
        when (RODZAJ_AKTYWÓW) = 'Kredyty'    then kl_nazwa
  as kraj
from corep
;

quit;

CodePudding user response:

Solution:

Should add

else
proc sql;
create table COREP as
select *,
    case 
                when (RODZAJ_AKTYWÓW) = 'Instrumenty pochodne' Then 'Polska'
                when (RODZAJ_AKTYWÓW) = 'Pozostałe aktywa'      then 'Polska'
                when (RODZAJ_AKTYWÓW) = 'Nostra' then 'Polska' 
                when (RODZAJ_AKTYWÓW) = 'Papiery wartościowe'       then 'Polska'
                else kl_kraj_opis   
                end     as Kraj
                from corep
                ;
                quit;

CodePudding user response:

In theory this should be the answer:

  • Use IN instead of multiple =
  • Add comma after *
  • Add END in CASE
proc sql;

create table COREP as
select (*)
  case 
        when (RODZAJ_AKTYWÓW) in ('Instrumenty pochodne', 
                                  'Pozostałe aktywa',    
                                   'Nostra',
                                   'Papiery wartościowe')    then 'Polska'
        when (RODZAJ_AKTYWÓW) = 'Kredyty'    then kl_nazwa
        else kl_kraj_opis   
        end  as kraj
from corep
;

quit;

Except for this in your original query:

"Kredyty hipoteczne"   as RODZAJ_AKTYWÓW   format $50. length=50,

Which sets all values in COREP for the variable to be a character constant which makes CASE useless as it will always be blank. I suspect that line is incorrect. If the variable had a space in it, you need an n at the end, ie

"Kredyty hipoteczne"n   as RODZAJ_AKTYWÓW   format $50. length=50,
  •  Tags:  
  • Related