Yes. Flip is right.
You also can make a variable to hold the format name. then use putn() or inputn() to make a transformation.
infile datalines dlm=',' dsd;
input range $ value ;
length format _value $ 20 ;
if range='1-5' then format='percent5.2';
else if range='test' then format='dollar10.2';
Does the column in question actually have such different values (something that would be a percent versus dollars)?
The short answer to your question, is: no, a given variable can only have one format associated with it at a time; however, I suspect that something else might be done if I understood the situation better. For example, although it would be a space hog, you could create a new column that was the appropriately formatted values of the original column.
There's a difference between associating a format with a variable in the descriptor portion of the SAS dataset and using a format for report purposes. For the purposes of the descriptor portion of the data set, a variable can only have 1 format assigned. If you want to create a new variable in a DATA step program, and the new variable value would be the result of using one format or the other, then you would use the PUT function, as shown. But the downside of this is that if the conditions change or the format changes, you have to go back and change the value of that new variable so it's in sync. The good news here is that the SAS dataset is not your only mechanism for getting what you want.
If you only needed the differing formats for reporting purposes, PROC REPORT will let you conditionally assign a format to one variable based on another variable's value using the CALL DEFINE statement. You would have a COLUMN statement and COMPUTE block something like this -- let's say you want to change the format for var3 based on the value of var2:
proc report data=mydata nowd;
column var1 var2 var3 var4;
.... more code ....
if var2 = 'Something' then do;
else call define(_COL_,'FORMAT','comma8.');
Your best bet for help on this is to look at the PROC REPORT documentation and search for some of the papers on PROC REPORT. Things might get a bit more complex if VAR2 were a GROUP or ORDER item on the report, but the general concept would still be the same.
I was researching for exactly this issue - displaying different formats for the same column in proc report. I tried your suggestion, but I have missing values for all the cells. I am not sure why -
If VISITS and PAGE_VIEWS are variables in the dataset, I don't know why you have them defined with a usage of "COMPUTED" -- you can still use them in a COMPUTE block even if they have a usage of DISPLAY. Also, without any ACROSS variables, the absolute column number of _C2_ is unnecessary. The simple reference _COL_ should work just fine.
The code below worked for me, given the fake data that I created from your description.
length DESC $30;
infile datalines dlm=',' dsd;
input DESC $ VISITS PAGE_VIEWS;
"Average Per Day",5.89,34.89
"Average Per Day Per Member",5.89,34.89
ods listing close;
ods html file='c:\temp\diff_fmt.html' style=sasweb;
PROC REPORT DATA=SITE_TRAFFIC_OVERVIEW nowd;
COLUMN DESC VISITS PAGE_VIEWS;
DEFINE DESC / order "Site Traffic" order=data;
DEFINE VISITS / Display "Visits";
DEFINE PAGE_VIEWS / display "Page Views";
IF DESC="Total" THEN DO;
ods html close;
as has been stated you cannot define more than one format to a column, but ...
an earlier post in the EG forum shows a way to transpose several variables (with different formats) into one column where their separate formats are preserved http://support.sas.com/forums/thread.jspa?messageID=48697븹
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.