DATA Step, Macro, Functions and more

put statement - format used contained in a variable

Reply
Contributor
Posts: 58

put statement - format used contained in a variable

Hi,

I was trying to run the following put in a data step

data b;

  length formattoapply $20;

input formattoapply $;

cards;

YYMMN4.

date9.

datetime20.

;

run;

data a;

  set b;

  newdate = put (today(), formattoapply);

run;

where formattoapply is a column of b that should contains differents formats for differents rows.

If I run this, I get ERROR 85-322: Expecting a format name.

Do there is a way to apply a different format for different rows where the format to apply is contained in a column?

many thanks.

regards

Respected Advisor
Posts: 3,777

Re: put statement - format used contained in a variable

that would the putN function in this case.

Regular Contributor
Posts: 195

Re: put statement - format used contained in a variable

Based on my knowledge, it is not possible to apply multiple format to different rows for variable...You can read the variable containing different dates structure as charcter variable and can fix it as charcter variable...

However, it will be good if you can share the purpose of your analysis by using this date variable in subsequent step...

Usually, variable types creates conflict in appending and joining (merging) SAS Datasets...

If your aim is structurize the same dates for all the rows then need to think out of box!!!

-Urvish

SAS Super FREQ
Posts: 8,743

Re: put statement - format used contained in a variable

HI:

  In a report (as opposed to a table), you can apply different formats to data cells on a report row using PROC REPORT and CALL DEFINE.

Cynthia

ods html file='c:\temp\diff_fmt.html';

proc report data=sashelp.class nowd;
  column name age sex ;
  define name / order;
  define age / display f=6.0;
  define sex / display;
  compute age;
    if name in ('Alfred', 'Alice', 'James') then do;
      call define(_col_,'format','9.3');
      call define(_col_,'style','style={background=yellow}');
    end;
  endcomp;
run;
ods html close;

Super Contributor
Posts: 1,040

Re: put statement - format used contained in a variable

Hi Cynthia,

i have a couple of questions?

Is call define used only in the compute block ????

it can eithwer be _col_ or _row_ in the call define..........cannot be both at the same time right?

Thanks

Respected Advisor
Posts: 3,777

Re: put statement - format used contained in a variable

Maybe you could take Cynthia's example and experiment.  Add a call define(_row_ and see what happens. 

SAS Super FREQ
Posts: 8,743

Re: put statement - format used contained in a variable

Hi:

There are some situations (such as changing background color) that are appropriate for a _row_; however, it would not be appropriate to use _row_ for a format, when the report row contains both character and numeric variables...a numeric format cannot be used with a character variable.

  The doc on CALL DEFINE is quite clear that you can have something other than _col_ or _row_, you can used a named variable, an absolute column number, and even an expression that resolves to a variable name. A CALL DEFINE statement can only be used in a COMPUTE block and only with PROC REPORT (not PRINT, not TABULATE).

cynthia

Frequent Contributor
Posts: 117

Re: put statement - format used contained in a variable

Hi,

We can apply multiple formats for a single variable as required. I faced this in my previous project to create multiple formats for each row.

You can see the attachment which i have created with multiple formats using Proc Report and call define as Cynthia said.

Thanks,

VishnuDifferent_formats_on_variable.jpg

Ask a Question
Discussion stats
  • 7 replies
  • 1439 views
  • 3 likes
  • 6 in conversation