How to use proc format with the number of lines?

149 views Asked by At

I have a table like this :

 |Num |  Label
-----------------------
1|1   |  a thing
2|2   |  another thing
3|3   |  something else
4|4   |  whatever

I want to replace my values of my label column by something more generic for example the first two lines : label One, the two next ones label Two ...

 |Num |  Label
-----------------------
1|1   |  label One
2|2   |  label One
3|3   |  label Two
4|4   |  label Two

How can I do that using proc format procedure ? I was wondering if I can use either the number of lines or another column like Num.

I need to do something like this :

proc format;
value label_f
low-2 = "label One"
3-high = "label Two"
;
run;

But I want to specify the number of the line or the value of the Num column.

2

There are 2 answers

0
Richard On

Gatsby:

It sounds like you want to format NUM instead of LABEL.

Where you want the use the 'generic' representation defined by your format simply place a FORMAT statement in the Proc being used:

PROC PRINT data=have;
  format num label_f.;
RUN;

If you want both num and generic, you will need to add a new column to the data for use during processing. This can be done with a view:

data have_view / view=have_view;
  set have;
  num_replicate1 = num;
  attrib num_replicate1 format=label_f. label='Generic';

  num_replacement = put (num,label_f.);
  attrib num_replacement label='Generic';   %* no format because the value is the formatted value of the original num;
run;

PROC PRINT data=have_view;
  var num num_replicate1 num_replacement;
RUN;

If you want a the 'generic' representation of the NUM column to be used in by-processing as a grouping variable, you have several scenarios:

  • know apriori the generic representation is by-group clustered
    • use a view and process with BY or BY ... NOTSORTED if clusters are not in sort order
  • force ordering for use with by-group processing
    • use an ordered SQL view containing the replicate and process with BY
    • add a replicate variable to the data set, sort by the formatted value and process with BY

A direct backmap from label to num to generic is possible only if the label is known to be unique, or you know apriori the transformation backmap-num + num-map is unique.

Proc FORMAT also has a special value construct [format] that can be used to map different ranges of values according to different formatting rules. The other range can also map to a different format that itself has an other range that maps to yet another different format. The SAS format engine will log an error if you happen to define a recursive loop using this advanced kind of format mapping.

propaedeutics

One of my favorite Dorfman words.

Format does not replace underlying values. Format is a map from the underlying data value to a rendered representation. The map can be 1:1, many:1. The MultiLabel Format (MLF) feature of the format system can even perform 1:many and many:many mappings in procedures many MLF enabled procedures (which is most of them)

To replace an underlying value with it's formatted version you need to use the PUT, PUTC or PUTN functions. The PUT functions always outputs a character value.

  • character ⇒ PUT ⇒ character [ FILE / PUT ]
  • numeric ⇒ PUT ⇒ character [ FILE / PUT ]

There is no guarantee a mapped value will mapped to the same value, it depends on the format.

INFORMATs are similar to FORMATs, however the target value depend on the in format type

  • character ⇒ INPUT ⇒ character [ INFILE / INPUT ]
  • numeric ⇒ INPUT ⇒ character
  • character ⇒ INPUT ⇒ numeric [ INFILE / INPUT ]
  • numeric ⇒ INPUT ⇒ numeric

Custom formats are created with Proc FORMAT. The construction of a format is specified by either the VALUE statement, or the CNTLIN= option. CNTLIN lets you create formats directly from data and avoids really large VALUE statements that are hand-entered or code-generated (via say macro)

Data-centric 'formatting' performs the mapping through a left-join. This is prevalent in SQL data bases. Left-joins in SAS can be done through SQL, DATA Step MERGE BY and FORMAT application. 1:1 left-joins can also be done via Hash object SET POINT=

0
user2877959 On

You could do what you are describing using the words format. You could swap out num for _N_ in the ceil function below in order to use the observation number instead of the value of num (if they are not always equal):

data have;
length num 8 label $20;
infile datalines dlm='|';
input num label $;
datalines;
1|a thing
2|another thing
3|something else
4|whatever
5|whatever else
6|so many things
;
run;

data want;
set have;
label=catx(' ','label',propcase(put(ceil(num/2),words.)));
run;

Although this answer is probably a bit too specific to your example and it may not apply in your actual context.