SAS Studio

Write and run SAS programs in your web browser
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 24804 views
  • 0 likes
  • 3 in conversation