BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello

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.

Thanks
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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 site:sas.com
Peter_C
Rhodochrosite | Level 12
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
PeterC

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
  • 2 replies
  • 738 views
  • 0 likes
  • 3 in conversation