BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASGeek
Obsidian | Level 7

Hello all,

I have a client that INSISTS that EVERYTHING be done in PROC SQL. So I need the following.

 

I have output that has both comma and numeric formatting in the same column. The client wants the results in a dataset (again, don't ask). 

 

The pos positions 1 & 2 need to be comma10. format, and pos 3 needs to be nlpcti10.2

 

descr  col 1 pos
Total        123,456 1
Change                123 2
% Change 45% 3

 

CAN that be done in PROC SQL? At least I have the flags so only pos = 3 needs the percent format, pos = 1, 2 need the comma10.

 

I tried using a CASE WHEN but it doesn't work. If I have to do it in a DATA step, OK, but they won't allow a proc format.

 

I have tried it in PROC REPORT but the output dataset doesn't hold the formatting nor the new variable names.  

 

HELP PLEASE! I'M DESPERATE

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Make the COL1 variable (what you called a COLUMN) a CHARACTER string.  Then you can store anything you want in it.

So let's assume the data is currently in a dataset named HAVE with variables named DESCR, POS and NUMVAR.  Then you could use SQL like this:

select descr
    , case when (pos=3) then put(numvar,percent6.) else put(numvar,comma10.) end as COL1
    , pos
from HAVE

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

No, it cannot be done with numeric variables. The format is a variable (or column) attribute and is the same throughout the dataset. You can only do this in a report, or by storing the formatted values in a character variable.

PaigeMiller
Diamond | Level 26

If you want a SAS data set with different formats on each row, I agree with @Kurt_Bremser it can't be done.

 

However, if you want a REPORT, then it is possible in PROC REPORT. But of course this violates the "do everything in PROC SQL" rule and also that you want the results as a SAS dataset.

 

So if you're desperate, I'm afraid you are going to remain desperate. Perhaps you can convince your client that sometimes SQL can't do everything.

 

You could use character variables, but of course then you can't perform arithmetic (like addition) or finding maximums from these character variables. (And then, it sounds like you want a report, not a data set)

--
Paige Miller
Tom
Super User Tom
Super User

Make the COL1 variable (what you called a COLUMN) a CHARACTER string.  Then you can store anything you want in it.

So let's assume the data is currently in a dataset named HAVE with variables named DESCR, POS and NUMVAR.  Then you could use SQL like this:

select descr
    , case when (pos=3) then put(numvar,percent6.) else put(numvar,comma10.) end as COL1
    , pos
from HAVE
SASGeek
Obsidian | Level 7

Thank you Tom.  Brilliant. It doesn't HAVE to result in numeric output, just it has to look numeric. BRILLIANT.

 

Thank you

mkeintz
PROC Star

First, I agree that this problem is best be solved in a PROC REPORT.

 

But if you must somehow only use SQL, you would have to place a constraint on subranges of the column.  For instance, if you choose to only use values of -1 through +1 to represent -100% through 100%, and use only other values to be presented in comma format, you could do this.

 

Take the sashelp.demographics dataset, with variables POP (actual population count) and POPURBAN (percentage represented as zero through one), put into the same column.

 

 

proc format;
  value pc  -1 - 1=[percentn10.2]
            other=[comma10.0];
run;

proc sql;
  create table want as 
  select name, 1 as row, pop as column_var format=pc10.2 from sashelp.demographics
  union
  select name, 2 as row, popurban as column_var  format=pc10.2 from sashelp.demographics
  order by name, calculated row
  ;
quit;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Quentin
Super User

Can you show the input dataset you have?

 

The goal is to make an output dataset with three columns and three rows?

 

Can we assume  that the Col1 variable is a character variable?

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1192 views
  • 3 likes
  • 6 in conversation