Vous êtes sur la page 1sur 11

Using SAS to Locate and Rename External Files

Lu Gan, Pharmaceutical Product Development, LLC, Austin, TX



ABSTRACT
WhenreadinginexternaldataintoSAS,aprogramneedstospecifythedatafilelocationandfilename
intherelatedimportingstatements.Iftherawdatafileisnamedwithatimestamp,theimport
programwillrequireanextrastepwhichisupdatingthefilenameeverytimebeforeitsrun.Thiswill
beahassleiftheimportisneededonaroutinescheduleand/ortherearemultipleexternaldatafiles.
Therefore,aSASprogramcanbewrittentoeliminatetherepeatingmanualupdate.Thisapproach
includestwosteps,firstsearchingforthelatestdatafileinthespecificfiledirectory,thenrenamingit
byremovingthespecifictimestamp.Afterexecutionoftheabovesteps,theimportprogramwillbe
abletoreadinthedatafilesdirectly.
TheexamplesandsyntaxillustratedareinSAS9.2.Thispaperassumesthatthereaderhasabasic
understandingofDATAstepprogrammingandthemacrolanguage.
INTRODUCTION
Timesavingisalwaysagoalprogrammersliketoachieveindailywork.Insteadofmanuallyrenaming
externaldatafiles,usingSAStoprogrammaticallydoitundoubtedlyimprovesefficiency.Thereare
varioustypesofexternaldatafilesSAScanimport;inthispapertextfile(.txt)isusedasanexample.

Theapproachstartswiththedefinitionsofseveralmacrovariableswhicharegoingtobeusedinthe
samplecode.

** ' f i l epat h' i s t he f i l e di r ect or y st or i ng t he f i l e( s) whi ch wi l l be r enamed;
%LET f i l epat h= %NRBQUOTE( U: \ SCSUG\ ) ;

** ' del i mi t er ' i s t he del i mi t er used i n f i l enames;
%LET del i mi t er = %NRBQUOTE( _) ;

** ' f i l e_ext ensi on' i s t he f i l e ext ensi on of t he ext er nal dat a f i l e( s) ;
%LET f i l e_ext ensi on= %NRBQUOTE( . t xt ) ;

** ' f i l ename_par t s' t el l s how many par t ( s) i n t he f i l ename excl udi ng f i l e
ext ensi on. Exampl e: i n f i l ename ' Sampl e_yyyymmdd_hhmm_Fi l en. t xt ' , t her e ar e 4
par t s del i mi t ed by t he del i mi t er ' _' ;
%LET f i l ename_par t s= 4;

** ' except _f i l e' i s a l i st of f i l e( s) t hat ar e wi t h same f i l e ext ensi on but
don' t want t o be r enamed;
%LET except _f i l e= %NRBQUOTE( Except 1, Except 2, Sampl e_Fi l e3) ;

F
s
G
T
s
e
f
F
D


R

T
o
Figure1belo
specifiedinm
GETALLFILE
Therearemu
statementas
externalfile,
folder.
FI LENAME i
DATA al l _f
I NFI LE
I NPUT f
RUN;
Thelogshow
observations
owtakesthe
macrovariab
E(S)INFORM
ultipleways
ssociatesaS
orlistsattri
i ndat PI PE
f i l e;
i ndat TRU
f l dt $ 1- 1
ws22record
sand4varia
efirstlookof
ble&f i l epa
MATIONINT
forSASprog
SASfilerefw
ibutesofext
E " di r " " &
UNCOVER;
11 f l t m$
swereread
ablesshown
fthetextfile
at h.
Fi g

HEFOLDER
gramstoint
ithanextern
ternalfiles.I
&f i l epat h.
12- 22 f l s
fromthef i
infigure2.
es(.txt)cont
gur e 1

teractwitht
nalfileoran
Itsusedher
*&f i l e_ex
i z $ 25- 3
i l er ef i nd
tainedinthe

heworldou
noutputdev
retogetallf
xt ensi on" "
8 f l nm$
dat .Thedat
esourcefold
utsideofSAS
vice,disassoc
file(s)inform
" ;
39- 100;
tasetal l _f
derwhichis
S.FILENAME
ciatesafiler
mationinthe
f i l e has22

refand
eabove
2

A
g
d
%

%

%

D





CLEANTHEF
Asyoumight
goingtober
dontwantt
%Cl ean_f i l

%MACRO Cl e

%LET numf =
DATA excep
LENGTH
%DO
%
f
O
FILE(S)INFO
talreadyno
removed.Be
orenameso
l enames_I n
ean_f i l ena
=%EVAL( 1+%
pt _f i l es;
H f l nm$60
i =1 %TO &
LET _f l nm
f l nm= STRI
OUTPUT;
RMATION
tice,dataset
esides,inma
owealsowa
nf oiscreate
ames_I nf o;
%SYSFUNC( C
0. ;
numf . ;
m= %SCAN( &e
P( " &_f l nm"
Fi g

tAl l _Fi l e
acrovariable
anttoexclud
edtodothe

COUNTC( " &e
except _f i l
" | | " &f i l e_
gur e 2

e contains
e&except _f
dethemfrom
cleaningw
except _f i l
l e. , &i , '
_ext ensi on
ssomeusele
_f i l e,there
mfurtherpr
work.
e. " , ' , ' ,
' , ' ) ;
n" ) ;
essobservat
eareseveral
rocessing.Th
t ) ) ) ;
tionswhicha
textfilesth
hefollowing

are
hatwe
macro

R

P
C
S
F
W


Q
%

A
3

W
f

D





%END
RUN;
PROC SQL;
CREATE TAB
SELECT *
FROM al l _f
WHERE NOT
ST

QUI T;
%MEND Cl ea
Aftermacro
3onlyconta

PROCESSTH
Withallthef
file(s)willbe
Sample_yyy
DATA f i l e_
SET al l
FORMAT
LENGTH
CALL MI
D;
BLE al l _f i
f i l e
( I NDEXC( f
TRI P( f l nm)
an_f i l enam
%Cl ean_f i
insusefulin
EFILENAM
fileinformat
erenamedto
yymmdd_hh
_i nf o;
l _f i l e_i nf
f i l e_dt Y
f i l et i me
I SSI NG( new
i l e_i nf o A
f l dt , ' Chec
NOT I N ( S
mes_I nf o;
l enames_I n
formationth
EINFORMA
tionavailabl
o.Inthisexa
mm_Filen.tx
f o;
YYMMDD10.
$5. new_f
w_f i l e_nam
AS
ck' , ' Di r ec
SELECT f l n

nf oisexecu
hatwillben
Fi g
ATION
e,wewantt
ample,weas
xtwillbere
f i l e_t mT
f i l e_name
me, f i l et i
ct or y' ) >
nmFROM ex
uted,output
eededinsu
gur e 3

tofollowna
ssumeorigin
namedtoS
TI ME5. ;
$50. ;
i me, f i l e_
0 OR MI SS
xcept _f i l e
tdatasetal
bsequentst

mingconve
nalfilename
Sample_Filen
_dt , f i l e_
I NG( f l dt )
es) ;
l l _f i l e_i n
eps.
ntiontofind
n.txt.
_t m) ;
) AND
nf oshowni
dthefilenam
nfigure
me(s)the




















R

F
f

f i l e_dt
I F I NDE
f i l e
ELSE
f i l e
f i l et i m
ARRAY f
DO i =1
f l na
I F I

END;
DO i =1
new_
END;
new_f i l
KEEP f l
RUN;
Fileinformat
filenameifa
t = I NPUT(
EX( f l t m, ' P
e_t m= I NP
e_t m= I NP
me= COMPRE
f l names{&f
TO DI M( f l
ames( i ) = S
NPUT( f l na
f l names(

TO DI M( f l
_f i l e_name
l e_name= S
l nmf i l e_d
tionissaved
afileisrenam
( f l dt , MMD
PM' ) THEN
UT( SUBSTR(
UT( SUBSTR(
ESS( PUT( f i
f i l ename_p
l names) ;
CAN( TRANWR
mes( i ) , ??
( i ) =' ' ;
l names) ;
= CATX( " &d
STRI P( STRI
dt f i l e_t m
dindataset
med.
DDYY10. ) ;

( f l t m, 1, 5)
( f l t m, 1, 5)
i l e_t m, ??
par t s} $20
RD( f l nm, "
?YYMMDD10.
del i mi t er "
I P( new_f i l
mnew_f i l e
f i l e_i nf o
Fi g
) , TI ME5. )
) , TI ME5. )
TI ME5. ) ,
. ;
" &f i l e_ext
. ) = f i l e_d
" , new_f i l
l e_name) | |
e_name;
whichinclu
gur e 4
) +43200;
) ;
' : ' ) ;
t ensi on" ,
dt OR f l na
l e_name, f
" &f i l e_e
udesfilenam
" " ) , i , "
ames( i ) = f
f l names( i )
xt ensi on"
me,dateand
" &del i mi t e
f i l et i me T
) ) ;
) ;
dtimeandt
er " ) ;
THEN
henew

N
t
t

D






R

D
Nowwefind
toberenam
thoseexistin
DATA del et
SET f i l
BY new_
I F FI RS
DO;
ELSE
DO;
RUN;
Datasetr en
Datasetdel
DELETEEXIS
doutwhichf
ediffilewit
ngfileswiths
t e_f i l es r
l e_i nf o;
_f i l e_name
ST. new_f i l
CALL SYMP
OUTPUT r e
name_f i l es
l et e_f i l es
ITINGFILE(S
filesaregoin
hsamenam
samename
r ename_f i l
e f l nmf i l
l e_name NE
UT( ' exi st i
name_f i l es
sincludesal
sincludesal
S)WHICHW
ngtoberena
mealreadyex
andflagthe
l es;
l e_dt f i l e
E LAST. new
i ng_f i l e' ,
s; END;
llexistingfile
Fi g
llexistingfile
Fi g
WILLBEREPLA
amed.Wind
xists,therefo
emfordeleti
e_t m;
w_f i l e_nam
, ' 1' ) ; OU
esthatwillb
gur e 5
esthatwillb
gur e 6

ACED
dowsopera
ore,beforer
ionwhileke
me AND new
UTPUT del e
berenamed
bedeleteda
atingsystem
renaming,w
eeptherestf
w_f i l e_nam
et e_f i l es;
.
andreplaced
mdoesntall
wewanttoid
forrenamin
me= f l nmTH
; END;
dthereafter.

owafile
dentify
g.
HEN

W
d

%















%

%
%
%




%
%

F
f

WeuseCALL
del et e_f i
%MACRO del
%I F %SY
%DO;
%SYS
%I
%D

%E
%E
%D
%E
%END;
%ELSE
%DO;
%P
%END;
%MEND del e
%MACRO del
%I F &exi st
%DO;
DATA _N
SET
CALL
RUN;
%END;
%MEND del e
Figure7isfr
filefoldersto
L EXECUTE
i l es :
l et e_ol d_f
YSFUNC( FI L
SEXEC DEL
F &SYSRC=
DO;
%PUT AL
END;
ELSE
DO;
%PUT A
&S
END;
PUT ALERT_
et e_ol d_f i
l et e_exi st
t i ng_f i l e=
NULL_;
del et e_f i
L EXECUTE(
et e_exi st i
omSASlogs
oresafterex
tocallmacr
f i l e( f i l e)
LEEXI ST( " &
" &f i l epat
0 %THEN
LERT_I : Fi
ALERT_R: F
SYSRC;
_R: &f i l epa
i l e;
t i ng_f i l e;
= 1 %THEN
l es;
' %del et e_o
i ng_f i l e;
showingwh
xecutionoft
ro%del et e_
;
&f i l epat h.
t h. &f i l e" ;
i l e &f i l e
Fi l e &f i l e
at h. &f i l e

ol d_f i l e( f
atfileshave
theabovem
Fi g
_ol d_f i l e
&f i l e" ) )

was succe
was NOT s
NOT FOUND
f i l e=' | | st
ebeendelete
macro.
gur e 7
todeleteth
%THEN
essf ul l y d
successf ul
D. ;
t r i p( f l nm)
ed.Figure8
hefilesstore
del et ed. ;
l l y del et e
) | | ' ) ; ' ) ;
displayswh
edindataset
ed. SYSRC=

hatfilestheo

t
=
original

R

A
c
f
m

P
B
R

D



R


P
RENAMETH
Aftertheabo
considerare
fileweassu
makesurefi
PROC SORT
BY new_f i l
RUN;
DATA t o_r e
SET
BY n
I F L
RUN;
PROC SQL;
EFILE(S)
oveproceed
:1>Ifmore
umethefile
le(s)in'&exc
DATA= r en
l e_name f l
ename_f i l e
r ename_f i
new_f i l e_n
LAST. new_f

dingsteps,fi
thanonefil
withthelat
cept _f i l e'
name_f i l es
l nmf i l e_d
es;
l es;
ame f l nmf
i l e_name;
Fi g
nallyweare
ewillberen
testtimestam
'willnotbe
s;
dt f i l e_t m
f i l e_dt f i
Fi g

gur e 8
ereadytore
namedtothe
mpinfilenam
renamed.
m;
i l e_t m;
gur e 9
namethefil
esamenam
meswillben
les.Couple
me,wewant
needed.2>
ofthingswe
tousethela
Checkagai

ewantto
atest
nto

C
S
F
W
Q


F
l

%
D









R
%

D


R

F
i
CREATE TAB
SELECT *
FROM t o_r e
WHERE new_
QUI T;

FunctionRen
ibrary,anen
%MACRO f i l
DATA _NULL
r c= REN

I F r c N
DO;
PUT
CALL
END;
ELSE
PUT
RUN;
%MEND f i l e
DATA _NULL
SET t o
CALL E
RUN;
Figure11 is
nthesamef
BLE t o_r en
ename_f i l e
_f i l e_name
name()isava
ntryinaSAS
l e_r ename(
L_;
NAME( " &f i l
NE 0 THEN
" ALERT_"
unsuccess
L SYMPUT( '
" ALERT_"
successf u
e_r ename;
L_;
o_r ename_f
XECUTE( ' %f
ST
fromSASLo
folderafter
name_f i l es
es
e NOT I N (
ailablesince
Scatalog,an
( f r om_f i l e
l epat h. &f r

" R: Renam
sf ul . " r c=
er r ' , ' 1' )
" I : Renam
ul . " ;
f i l es2;
f i l e_r enam
TRI P( new_f
ogshowing
allproceedi
s2 AS
( SELECT f l
Fi g
eSASversion
externalfile
ename, t o_
r om_f i l ena
me of f i l e
=;
;
me of f i l e
me( f r om_f i
f i l e_name)
whatfilesa
ngsteps.
l nmFROM e
gur e 10
n9.2.Itspur
e,oradirect
_f i l ename)
ame" , " &f i
e &f r om_f i
e &f r om_f i
i l ename=' |
| | ' ) ; ' ) ;
redeletedo

except _f i l
rposeistore
tory.Itisuse
;
l epat h. &t
l ename t o
i l ename t o
| | STRI P( f l
orrenamed.
es) ;
enameame
edheretore
o_f i l enam
&t o_f i l e
o &t o_f i l e
l nm) | | ' , t
Figure12d
emberofaSA
enamethef
me" , ' f i l e'
name
ename
t o_f i l enam
displaysthe

AS
ile(s).
' ) ;
me=' | |
textfiles

C
T
a
o
a
S
s

R
S
S

A

CONCLUSION
Thispaperd
alongwithse
othernecess
activities.Th
SASsystemb
solutionstot
REFERENCES
Schacherer,
SAS(R)9.2La
ACKNOWLED
N
escribedaw
everalsystem
saryproceed
epurposeo
beyondbasic
thedaytod
S
C(2011)The
anguageRef
DGEMENTS
waytolocate
mfunctions
dingandsuc
ofthispaper
cdatamanip
dayprogram
eFILENAME
ference:Dict
Fi g
Fi g
eandrenam
andcomma
ceedingstep
istryingto
pulationsoa
mmingactivit
Statement:
tionary,Four

gur e 11
gur e 12
meexternalfi
ands.Withth
pstoabatch
presentthe
astoachieve
ties.
Interactingw
rthEditionC
ile(s)usingS
heautomati
hjobtofully
ideaofexpa
emoreeffic
withthewo
Cary,NC:SAS
SASdataste
ionofrenam
yautomatee
andingthec
cientandmo
rldoutsideo
SInstituteIn
ps,macrola
ming,usersc
externaldat
capabilitieso
oreautomat
ofSAS
nc.

anguages
canadd
aimport
ofthe
ed
IdliketothankmycoworkerRanjanKarmakarfordiscussingwithmewhenIhadtheideaaboutthis
approach.IalsoappreciatethesupportfromtheprogrammingmanagementteamatPharmaceutical
ProductDevelopment,LLC.

SASandallotherSASInstituteInc.productorservicenamesareregisteredtrademarksor
trademarksofSASInstituteInc.intheUSAandothercountries.indicatesUSAregistration.Other
brandandproductnamesareregisteredtrademarksortrademarksoftheirrespectivecompanies.

CONTACTINFORMATION

Yourcommentsandquestionsarevaluedandwelcomed.Pleasecontacttheauthorat:

LuGan
PPD
7901E.RiversideDrive
Austin,TX78744
Lu.Gan@ppdi.com
(512)7475715

Vous aimerez peut-être aussi