DATA Step, Macro, Functions and more

formats

Reply
Occasional Contributor
Posts: 5

formats

Dear all

Is there any posibilty of using "format statement" in if codition (Or) do loops, if so please explain me with example.

Thanks

Harikrishna

Super User
Posts: 5,254

Re: formats

No. You need to use macro programming logic to execute it conditionally.

Data never sleeps
Super User
Posts: 17,724

Re: formats

In general no, but if you're trying to dynamically recode a variable take a look at putC and putN functions which take an argument as the second parameter. However, they will be character variables reflecting the formatted variables not the underlying variables.

A variable can have only one format assigned so if the entire variable is taking a different format in different circumstances then you can use macro logic.

Occasional Contributor
Posts: 5

Re: formats

Thanks alot for your replies,

Could you explain me, macro logic taking sashelp.class writing condition on sex and formatting the values of height with two different formats.

Thanks

Super User
Posts: 5,254

Re: formats

You can't have two separate formats for one variable within one data step/procedure.

By using macro logic you can dynamically chose the format depending on a specified condition (%IF - see on-line doc, or take some training/tutorial).

If you for some reason what to chose a format on an observation level you might want to examine your whole report/data management process (or even you data model).

Data never sleeps
Super User
Posts: 17,724

Re: formats

Post an example of what you want your output to look like.

SAS Super FREQ
Posts: 8,739

Re: formats

HI:

  What you want to do is possible with PROC REPORT, but not possible with a data. A dataset can only have 1 format applied to a variable column. But, a REPORT can have different formats applied to different report rows using CALL DEFINE. See the code below as an example. Note that this will impact what you see on each report row, but does not impact how the data is stored internally.

Cynthia

proc report data=sashelp.class nowd;

  column name age sex height weight;

  define name / order;

  define age / display;

  define sex / display;

  define height / display;

  define weight / display;

  compute weight;

    if name = 'Alfred' then

       call define(_col_,'format','12.3');

    else if substr(name,1,1) = 'J' and sex = 'F' then

       call define(_col_,'format','12.5');

    else call define(_col_,'format','12.0');

  endcomp;

run;

Occasional Contributor
Posts: 5

Re: formats

Thanks all,

I am writing sas strored process to bring data from sas olap cube and writes to the _webout, The strored process has measure  parmeter , based on measure selection the strored process brings the data from cube. I have controll table which has list of measures and formats to the repective measures in seperate colums. My requirement is based on the measure slection the related measure format is applied and writes to the _webout(this is beacuse the client wants to controll formats from control table)

parmeter value can be passed for single, double and triple  measures from the control table.

data table:-

productname    measure        2005        2006      2007

a                       price                 25           25          25

a                       quantity            10           20            5

a                       totalsale         250          500        125

b                       price                30            30        30

b                      quantity            10             20       30

b                      totalsale         300          600         900

c                      price               40              40        40

c                     quantity             10           20        5

c                    totalsale            400         800       200

control table:-

Measurename   Measureformat

price                  5.2

quantity             4.2

totalsale            6.2

Thanks.

Super User
Posts: 5,254

Re: formats

Building a stored process makes this task probably a bit more difficult. Why not use an OLAP viewer and use the cube formats?

But if you use PROC TABULATE or PROC REPORT (as exemplified by ) you can still have formats on each measure column, and let the PROC do the lay-out.

If you endure with a data set based solution, you need to put the value using a specied format, and store the result in a char column. Doable, but not good looking...

Data never sleeps
Occasional Contributor
Posts: 5

Re: formats

Yes, I agree with olap viewer and cube formats in my case stored process deployed as soap webservice to use it in java applications for presentation.

Hence I cann't use the above given procedures also.

Thanks.

Super User
Posts: 5,254

Re: formats

Then I suggest that you deliver the data extract in normalized format, and let the GUI people handle the lay-out.

Use the principles of layered architecture.

Data never sleeps
Super User
Posts: 17,724

Re: formats

The problem is with how your storing thr data. Which isn't conducive to updates either.

Try putting your variables (sales, quantity, total sales) as columns and years as rows 

Ask a Question
Discussion stats
  • 11 replies
  • 398 views
  • 0 likes
  • 4 in conversation