BookmarkSubscribeRSS Feed
juan94
Calcite | Level 5

Buena Tarde,

Como hago para que en la columna "A" tengo la información para calcular, esta cuenta con AÑO, MES, COD RAMO, RAMO, CUENTA FORMATO, NOMBRE, COMPAÑIAS, me gustaría calcular una Columna "B", de la siguiente manera:

CASO
CUANDO t1.A = 0
LUEGO "BUSCAR VALOR ANTERIOR"  
CASO
CUANDO t1.MES = 1
LUEGO t1.A
ELSE t1.A
END
END}

 

AÑOMESCOD RAMORAMOCUENTA FORMATONOMBRECOMPAÑIASNaciones Unidassegundo
2017111hacer10999TOTAL PARCIALMETRO-596842008.2-596842008
2017211hacer10999TOTAL PARCIALMETRO-15616722.71-15616722.7
2017311hacer10999TOTAL PARCIALMETRO0-15616722.7
2017411hacer10999TOTAL PARCIALMETRO0-15616722.7
2017511hacer10999TOTAL PARCIALMETRO0-15616722.7
2017611hacer10999TOTAL PARCIALMETRO0-15616722.7
2017711hacer10999TOTAL PARCIALMETRO-2338748.87-2338748.87
2017811hacer10999TOTAL PARCIALMETRO0-2338748.87
2017911hacer10999TOTAL PARCIALMETRO-40651243.2-40651243.2
20171011hacer10999TOTAL PARCIALMETRO-999345.29-999345.29
20171111hacer10999TOTAL PARCIALMETRO-1388093.24-1388093.24
20171211hacer10999TOTAL PARCIALMETRO-1994599.5-1994599.5
1 REPLY 1
ChrisHemedinger
Community Manager

Hola @juan94, bienvenido a SAS Support Communities.  Yo comprendo español un poco, pero hablamos ingles en este foro.

 

And...SAS syntax is based in English...

 

Your pseudocode (using a CASE statement) requires SQL.  Is that how you intend to solve this?  If so, the form of an SQL CASE statement is:

 

  CASE (term | expression)

    WHEN (compare-value | expression) THEN (result-value)

    ...

  END as (target-variable)

 

So your example would be:

 

proc sql;
  create table target as select *,
   case t1.A 
     when 0 then "BUSCAR VALOR ANTERIOR"
     else "default value"
   end as new_value1,

  case t1.MES
   when 1 then t1.A 
   else t1.A 
  end as new_value2
  from source_table;
quit;   

Of course, second expression does not make sense, as it results in the same value whether for any condition of t1.MES (month).  I'm not really sure what your objective is here -- but I might have missed something in my poor translation.

 

If you're new to SAS and using SQL syntax because that's what you're familiar with, I suggest trying with DATA step instead.  That approach is much more flexible.  Check out this tutorial, How to Write a Basic SAS Program.  It's in English, but I think you can turn on captioning and that might help.

 

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1296 views
  • 2 likes
  • 2 in conversation