使用通配符时SAS如何选择目标文件?
INFILE Great question—this is a common point of confusion for SAS users working with wildcard file imports! Let’s break down exactly what’s happening with your code:
Core Rule: SAS Delegates to Your Operating System
SAS doesn’t handle sorting or prioritizing files when you use a wildcard like * in your INFILE statement. Instead, it relies entirely on your underlying operating system (Windows, in your case, given your M:\YPATH path) to provide the list of matching files.
When SAS encounters the wildcard pattern, it asks the OS to return all files that fit. By default, SAS will read files in the exact order the OS provides that list. Since your code always imports the latest file, that means your OS is returning the newest date-named file as the first entry in its matched list.
Why Your Naming Convention Makes This Work
Your file names (myfile_150.YYYYMMDD) use a lexicographically sortable date format—this is the key to why your current setup works. When comparing strings character by character:
myfile_150.20240521is treated as "larger" thanmyfile_150.20240520
Most operating systems (including Windows) can be configured to sort file lists by name in descending order. If your system is set up this way, the newest date file will be at the top of the matched list, so SAS picks it first.
If your OS used ascending name order instead, the oldest file would be first, and your code would fail to import the latest one. The fact that it works confirms your OS is returning files in descending name order.
Critical Notes for Reliability
- No Built-In SAS Sorting: SAS never sorts the file list on its own. If someone adjusts your OS’s file listing settings, your code might suddenly import the wrong file.
- Multiple Files Get Concatenated: If you ever have multiple files matching the wildcard, SAS will read all of them consecutively and combine them into one dataset. If you only want the latest file, relying on wildcard order isn’t 100% safe long-term.
- Explicit Control Alternative: To guarantee you always get the latest file, regardless of OS settings, you can:
- Use a
FILENAMEstatement with theDIRoption to generate a list of all matching files. - Sort that list by the embedded
YYYYMMDDdate (or file modification time) in SAS. - Use the
FILEVARoption inINFILEto target the latest file explicitly.
- Use a
内容的提问来源于stack exchange,提问作者CrunchyCruncha

