Formatting observations

Regular Contributor
Posts: 215

Formatting observations

Can anyone tell me the code how to format/update observations.

I have the following dataset:

Name  Group

John  MK 3

Ciara MK 4

Jack LR 9

Alam MK 5

Mike LR 8

basically I want to create a new variable and group the onservation as MK (where it start with MK%) and LR (where it start with LR%) which should look like:

Name  Group Group2

John  MK 3  MK

Ciara MK 4  MK

Jack LR 9   LR

Alam MK 5  MK

Mike LR 8   LR

How do I do it with format and how do I do it without formating?

Super User
Posts: 10,500

Re: Formatting observations

Format works with a list or range of values. Since character values do not behave as most people want when attempting to use ranges you generally have to list every value you expect to encounter. Something like

Proc format;

value $ group

"MK 1","MK 2","MK 3", "MK 4" = "MK"

"LR 8", "LR 9" = "LR"



Then to create a new variable in a datastep:

data want;

set have;

Group2 = put(group,$group.);


One major advantage of formats is that you may not  need to create an additional format.

You are likely to be better off with string manipulation functions such as SUBSTR. Again in a data step:

data want;

set have;

Group2 = upcase(substr(group,1,2));


Super User
Posts: 5,085

Re: Formatting observations

If all of your values begin with MK or LR, you could try:

data want;

   set have;

   length group2 $ 2;

   group2 = group;


Since GROUP2 is defined as having a shorter length, it only has room to store the first two characters of GROUP.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation