BookmarkSubscribeRSS Feed
HariKrishna
Calcite | Level 5

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

11 REPLIES 11
LinusH
Tourmaline | Level 20

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

Data never sleeps
Reeza
Super User

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.

HariKrishna
Calcite | Level 5

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

LinusH
Tourmaline | Level 20

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
Reeza
Super User

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

Cynthia_sas
SAS Super FREQ

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;

HariKrishna
Calcite | Level 5

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.

LinusH
Tourmaline | Level 20

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
HariKrishna
Calcite | Level 5

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.

LinusH
Tourmaline | Level 20

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
Reeza
Super User

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 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 11 replies
  • 2206 views
  • 0 likes
  • 4 in conversation