Help using Base SAS procedures

loop and creating date format

Not applicable
Posts: 0

loop and creating date format


I have a library with datasets for jan to dec for 2009. Within each dataset is a variable called filedate whose type is numeric and gives date in the form of 20090131.

I would like to know how to programm such that I can run a loop to read the files one by one from the library for all 12 months and create a new variable called date which should have a sas date format showing me the date as 200901 - so basically it should give me the year and the month.

Super Contributor
Super Contributor
Posts: 3,176

Re: loop and creating date format

Posted in reply to deleted_user
Your SAS version and OS platform is not clear with your post - it is always useful to provide this information - suggestion for next time.

You can use the FILENAME statement with the PIPE access method to define your input file naming criteria. And you can use the FILEVAR= parameter on the INFILE statement to identify each incoming file name so you can assign a SAS numeric DATE variable and use a suitable output FORMAT to display the date as desired (you mentioned yyyymm).

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search argument this topic/post:

filename pipe read files directory
Valued Guide
Posts: 2,191

Re: loop and creating date format

Posted in reply to deleted_user
If these are sas datasets in a sas library, then you could use the SAS9.2 option for SET statements INDSNAME= which names a variable to hold the name of the dataset which contributes to that iteration.
Supposing all of that and your data sets are named Tcrat01 - Tcra12 and contain accounting data sorted by ac_no, then this selects only ac_no beginning 'ABC' (at the same point you can use some more relevant filter) but places their rows in account and fileDate order ;[pre] data selected ;
set yourlib.tcrat1-yourlib.tcrat12 INDSNAME= in_table ;
by ac_no filedate ;
where ac_no =: 'ABC' ;[/pre]* now convert number like date into sas date ;[pre] date = input( put( filedate, z8. ), yymmdd8.) ;
format date yymmN6. ;[/pre]* now copy the indsn= info to another variable to keep it in the output table;[pre] from_set = in_table ;
run ;[/pre]
If your "library" is not a sas library but a folder or pds (on mvs) then there is some similar functionality on the INFILE statement, for handling external files, like FILENAME= and FILEVAR=, but I hope we're talking sas datasets here.

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