Date: Tue, 8 Jan 2008 15:24:29 -0500
Reply-To: "Fehd, Ronald J. (CDC/CCHIS/NCPHI)" <rjf2@CDC.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Fehd, Ronald J. (CDC/CCHIS/NCPHI)" <rjf2@CDC.GOV>
Subject: Re: inserting (not replacing!) characters into strings
In-Reply-To: <f3ed116f0801081205m1d19f263i42f6a64c6396aced@mail.gmail.com>
Content-Type: text/plain; charset=us-ascii
> From: Whitworth, William C. (Kit)
> Subject: inserting (not replacing!) characters into strings
>
> 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.
> From: bruce johnson
> It may not be the most elegant, but it works...
> data temp;
> patientid="28300102";
> patid=substr(patientid,1,2)||"-"||substr(patientid,3,2)||"-"||
> substr(patientid,5);
> put _all_;
> run;
>
> PATIENTID=28300102 PATID=28-30-0102 _ERROR_=0 _N_=1
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,3,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
|