inserting (not replacing!) characters into strings



  • 1. Looking for a DATETIME format...
    I have a DATETIME18 field in a SAS dataset that I want to convert to m/ d/yyyy X:XX XM format. For example, I have a date and time stored as 29JAN01:09:25:48 in a SAS dataset but I would like this date shown as: 1/29/2001 9:25 AM. Is there a DATETIME (or some other) format for this? If not, is there a way to get this date and time field shown how I want it?
  • 2. Hosmer-Lemeshow Code
    I am testing a risk model that outputs an estimate of 1-year survival (SHFM1). The observed 1-year outcomes are binary (out_365). I would appreciate some help with the sas code, as I cannot find an example online or in this group (only discussions of the model(s) ). Currently, I have: proc logistic data=newhl; class shfm1 out_365; model shfm1=out_365/lackfit; run; Is this correct? I am specifically looking for the HL GoF test, which I beleive is "c" on the output. Thanks, Todd
  • 3. Proc Lifereg Hazard
    Two quick survival analysis questions 1) I have time to event data which is measured on a day scale (event happens 1 day from start or 2 days or....n days) and can be heavily tied. Using proc lifereg (parametric model) should I consider this discrete data and use interval censoring? 2) Using proc lifereg, I can output cdf (output out=table cdf=c) and from this I can get the survivor probability (1- cdf) but is there a way to get the hazard rate (or in my case with discrete data the hazard probability)? Thanks!
  • 4. Time is AM, instead of PM, when using ODS RTF
    To -- I'm using ODS RTF to write SAS/GRAPH results to an RTF file. The date- time stamp that gets written in the header of the RTF file is: 05:33 Wednesday, April 09, 2008 The only problem is, that the time is really 5:33 PM -- so it should say either "5:33 PM" or (preferably) "17:33". Does anyone know how to fix this? Thanks much! --Tom

inserting (not replacing!) characters into strings

Postby wcw2 » Thu, 10 Jan 2008 05:00:16 GMT

I have eight digit (character format) patient IDs ("PT") (ex: 28300102)
that I need to insert hyphens into to give me 28-32-0102. I tried the
substr function (for the first hyphen only) [substr (PT, 3, 1) = "-";], but
this will replace the original value with a "-" rather than inserting the
hyphen (i.e., it gives "28-00102" instead of "28-300102"). Any help would
be greatly appreciated! Hope this was clear.

Re: inserting (not replacing!) characters into strings

Postby wcw2 » Thu, 10 Jan 2008 05:02:55 GMT

sorry....that should be "28-30-0102" on the 2nd line

Re: inserting (not replacing!) characters into strings

Postby dave » Thu, 10 Jan 2008 05:05:39 GMT

Use a (v9) CAT function, with substr() on the right-hand side:

newPT = catX('-', substr(PT, 1, 2), substr(PT, 3, 2), subStr(pt, 5)) ;


Re: inserting (not replacing!) characters into strings

Postby chimanbj » Thu, 10 Jan 2008 05:05:40 GMT

It may not be the most elegant, but it works...

data temp;
put _all_;

PATIENTID=28300102 PATID=28-30-0102 _ERROR_=0 _N_=1

Re: inserting (not replacing!) characters into strings

Postby tobydunn » Thu, 10 Jan 2008 05:14:43 GMT

PT = CatX( '-' , Substr( PT , 1 , 2 ) , Substr( PT , 3 , 2 ) , Substr( Pt , 5 ) ) ;

Toby Dunn

"Don't bail. The best gold is at the bottom of barrels of crap."
Randy Pausch

"Be prepared. Luck is where preparation meets opportunity."
Randy Pausch

Watch ause Effect,a show about real people making a real difference.

Re: inserting (not replacing!) characters into strings

Postby datanull » Thu, 10 Jan 2008 05:18:48 GMT

It would be nice if we had picture formats for character values.  If
your ids are all digits the you can convert to numeric and use PICTURE

proc format;
   picture PT other = '99-99-9999';
data _null_;
   do id = '28300102','02301111';
      ptid = put(input(id,f8.),pt10.);
      put (_all_) (/=);

Re: inserting (not replacing!) characters into strings

Postby rjf2 » Thu, 10 Jan 2008 05:24:29 GMT

> From: Whitworth,  William C. (Kit)

You can also use the New&Improved cat function
the first argument is the delimiter to insert between successive strings

PatId = catx('-',substr(patientid,1,2)
                ,substr(patientid,5  ));

if the PatientId were numeric,
which would be appropriate since it is a PrimaryKey,
then you could use a picture format to do this

RTFM on The Format Procedure, Picture Statement

Ron Fehd  the macro maven  CDC Atlanta GA USA RJF2 at cdc dot gov

Re: inserting (not replacing!) characters into strings

Postby wcw2 » Thu, 10 Jan 2008 21:09:06 GMT

Hey thanks so much y'all, this is a great help! : )

Re: inserting (not replacing!) characters into strings

Postby yingtaoliu » Fri, 11 Jan 2008 00:40:41 GMT

Data_null's picture format is a good way. I played with the code by
modifying as following, and

and found *2* issues:

1.* The Character values have been converted to numeric values;

2.* Due to *1*, the actual data set does not keep the format when you export
(e.g proc export to excel);

3.* if I put format ptid pt10. after ptid = put(input(id,*8.*),*10.*), I got
error " The format $PT was not found or could not be loaded.

", why the order matters?


data* b;

do id = '02301111';

format ptid pt10.;

ptid = put(input(id,*8.*),*10.*);

put (_all_) (/=);



Re: inserting (not replacing!) characters into strings

Postby datanull » Fri, 11 Jan 2008 01:15:49 GMT

I don't think you understand my program.  I thought since the poster
wcw2 indicated that subject ID was a character variable that it should
stay character.  My program created a new variable from the original
subject ID character variable.

data _null_;
  do id = '28300102','02301111';
     ptid = put(input(id,f8.),pt10.);
     put (_all_) (/=);

PTID is the NEW version of ID with the dashes "inserted".   The new
variable PTID would replace the old ID.  PTID should, I believe,
export to XL properly formatted. This only works if ID contains only

 If subject ID was numeric then no new variable would be needed and
you could simply format numeric subject ID with PT10. format.

346  data _null_;
347     do idN = 28300102,02301111;
348       put (_all_) (/=);
349       end;
350     format idN pt10.;
351     run;



Similar Threads:

1.How to delete (replace) specific characters in an irregular string


data t1;
x='ab, 6, c, def';
y='h, i, jk';

Is there a simple way to knock out the ','s in x and y , that is to
replace ',' with 0 length empty place.  As you see, x and y have irregular
patterns.  The SUBSTR(Left of =) function needs specific character
positions, and the approach using INDEX() and SCAN() fucntions seems to be
too complicated.  Thanks!

2.How to insert a single literal quote (") character in a string

Suppose one wants to write this string, an html tag, in an output

     <a name="section"></a>

The first step is to create a variable with this value.  It seems like
this line would work:

     tag = '<a name=' || ? || 'section' || ? || '></a>' ;

but what goes in place of the ? above?

Thanks in advance.

John Uebersax

3.gsfmode=replace does not replace

I am using the goption gsfmode=replace and I am expecting that if you
use name="xxx" on the ganno statement then when you come to run it a
second time it will "replace" what it wrote before and reuse the name
"xxx". But it does not. This is true for sas v8.2 and sas v9.1.3 .
What is wrong? How can I get it to reuse the name I have defined to
name= on "proc ganno"?

4.translate the _entire_ string, not the characters

hi SAS-L

My goal is to, without having to use INDEX*(), but using maybe
TRANSLATE() or some other function, to replace the '"' value in a
given variable (&&var&i below) for a single character quote mark.  If I
use newvar = translate(&&var&i,'"','"') then all instances of each
of the characters '&','q','u','o','t' and ';' are dropped.  The same
goes for '<br>': I simply want to replace this entire character
sequence with a space, but I seem to replace all instance of each of
the characters '<','b','r','>'.

I am interested in not using the index family of functions, since the
variable's values may or may not include 0, 1, or more instances of

As well, since this is data stored in a SQL Server, I would rather not
write out to a flatt file in order to process using the @@ and then
read back in for additional processing.

Thanks in adnvance,

5.Replacing a Character variable with part of another Character

> -----Original Message-----
> From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of SAS
> Student
> Sent: Saturday, December 08, 2007 2:24 PM
> Subject: Replacing a Character variable with part of another Character variable
> Good evening all,
>  I have a small problem that I need to solve and would appreciate any
> help you offer.
>  I created the below table using some raw data. I created the Location
> variable using the
>  ZIPCITY function. As you can see, in the last observation the City
> "Harrisbu" does not match the city under Location "Levittown" while
> for the first observation the Location is left blank because the
> zipcode is invalid.

It will be a lot easier for people to help you if you provide a self-contained program that generates your sample data, rather than making them try to extract data from a poorly formatted email (everything is wrapping haphazardly at my end of the email).

That being said, is this just a toy problem or is this a real problem you are trying to solve.  I ask, because ZIP codes are not geographical regions, but rather mail delivery routes.  There are many places where the zipcode "location" does not match the town.  In fact, sometimes even the state won't match.  So I'm not convinced you should be editing anything.

>I need a simple code that does the following:
> 1.If they zipcode does not match the city/state (6th observation),
> create a code that suggests either edits to the zipcode or city/state.

What do you mean by create a code?  It would be helpful if you took the sample data from your email and showed us what you would like the end result to be.

> 2. If the Zipcode is invalid (1st observation) i need to replace its
> value with "???"

How do you know that a ZIPCODE is invalid?  Because location is blank?  If you write back to SAS-L and show us what you want to end up with, someone will surely be able to give you some useful help (maybe even me :-).

Daniel Nordlund
Bothell, WA USA

6. Replacing a Character variable with part of another Character variable

7. find and replace strings

8. How to replace certain characters in a variable with '_'

Return to sas


Who is online

Users browsing this forum: No registered users and 76 guest