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

I need to have one numeric column newvar with max length of 12 and 2 decimal places

PROC SQL;

   CREATE TABLE newtable AS

   SELECT

      (CASE
         WHEN oldvar=1 THEN oldvar2 * 123.45
         WHEN oldvar=2 THEN oldvar2
      END) newvar

   FROM oldtable;

QUIT;

I've tried with newvar format=12.2 and DECIMAL(oldvar * 123.45,12,2) with no success.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Describe what you mean by "no success"?

 

You do not need, or likely want parentheses around a case statement; any assignment in a SELECT statement should have AS varname so SQL knows what variable to assign the value to.

PROC SQL;
   SELECT
      CASE
         WHEN oldvar=1 THEN oldvar * 123.45
         WHEN oldvar=2 THEN oldvar
      END as newvar format=12.2
   FROM oldtable;
QUIT;

But the value 123.45 is not going to be much of a test for 12.2 format. A format is not necessarily going to pad the displayed length to 12 characters if the value uses fewer character positions.

 

Note that this shows that the format is applied and the decimal portion of the display only uses 2 decimal positions and rounds the result to fit:

PROC SQL;
   SELECT
      CASE
         WHEN oldvar=1 THEN oldvar * 123456.876543
         WHEN oldvar=2 THEN oldvar
      END as newvar format=12.2
   FROM oldtable;
QUIT;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are not creating a new dataset with that statement:

proc sql;
  create table want as
  select case when oldvar=1 then oldvar * 123.45
              when oldvar=2 then oldvar end as newvar format=12.2
  from   oldtable;
quit;
                     

In the table want, you will see newvar with the format applied.  Do note you have no else, so anything other than 1 or 2 will be ignored.

ballardw
Super User

Describe what you mean by "no success"?

 

You do not need, or likely want parentheses around a case statement; any assignment in a SELECT statement should have AS varname so SQL knows what variable to assign the value to.

PROC SQL;
   SELECT
      CASE
         WHEN oldvar=1 THEN oldvar * 123.45
         WHEN oldvar=2 THEN oldvar
      END as newvar format=12.2
   FROM oldtable;
QUIT;

But the value 123.45 is not going to be much of a test for 12.2 format. A format is not necessarily going to pad the displayed length to 12 characters if the value uses fewer character positions.

 

Note that this shows that the format is applied and the decimal portion of the display only uses 2 decimal positions and rounds the result to fit:

PROC SQL;
   SELECT
      CASE
         WHEN oldvar=1 THEN oldvar * 123456.876543
         WHEN oldvar=2 THEN oldvar
      END as newvar format=12.2
   FROM oldtable;
QUIT;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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