BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

Hi

 

I am new to SAS, so pardon if my question is simple.

 

I would like to format all my character variables that starts with certain characters. E.G

 

1) 'ta'

2) '4' (I have numbers that that are stored as character variables)

 

How do I do that?

 

Thank you

3 REPLIES 3
Patrick
Opal | Level 21

Format or recode? If it's format then your question is actually less trivial than you might think.

 

Can you give us a bit a more extensive sample like a few source strings and to what you would like them formatted or recoded?

apple
Calcite | Level 5

Hi PAtrick,

 

Thanks for the reply. It's format.

 

Eg,

I may have under the variable Name:

 

'Tan Peter'

'Tan John'

'Mike Tan', ....

 

I could have many names containing Tan.

 

I would like to format all Names containing Tan

into 'the Tans' without listing out all the Names.

How can i do so?

 

Thank you

 

Patrick
Opal | Level 21

Hi @apple

 

The difference between "recoding" and "formatting":

With recoding you modify the actual internal value of a variable and then assign the result to a new variable.

With formatting you don't change the internal value but just change the way this value gets printed.

 

I believe you should go for "recoding". Below an example illustrating how this works.

data have;
  infile datalines truncover;
  input name $20.;
  datalines;
Tan Peter
Tan John
Mike Tan
John Doe
Tanner Tolbert
;
run;

/* recode */
data want_recoded;
  set have;
  length name_rec $20;
  if findw(upcase(name),'TAN',' ') then name_rec='the Tans';
  else name_rec=name;
run;

The challenge you're facing with names are different spellings and variations (like Bill and William). With names you normally need first to apply some sort of standardization process.

 

Below the code sample for "formatting". This code is "advanced" and not what you should be concentrating on if you're "new to SAS".

data have;
  infile datalines truncover;
  input name $20.;
  datalines;
Tan Peter
Tan John
Mike Tan
John Doe
Tanner Tolbert
;
run;

/* format */
proc fcmp outlib=work.funcs.name_recode;
  function name_recode(in_name $) $;
    length out_name $20;
    if findw(upcase(in_name),'TAN',' ') then out_name='the Tans';
    else out_name=in_name;
    return(out_name);
  endsub;
run;

options cmplib=work.funcs;
proc format;
  value $name_group (default=20)
    other = [name_recode()]
  ;
run;

proc print data=have;
  var name;
  format name $name_group.;
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 888 views
  • 0 likes
  • 2 in conversation