如何在SAS系统中添加或删除空白列?(第19列调整需求)
Got it, let's fix this fixed-width data alignment issue for your SAS program. The core problem is your raw data isn't matching the column positions defined in your INPUT statement, especially around the 19th column where the team name ends and numeric values begin.
Understanding Your Current Setup
Your SAS code defines fixed columns for each variable:
VisitingTeam $ 1-20: Team name occupies columns 1 through 20ConcessionSales 21-24: Numeric sales data starts at column 21BleacherSales 25-28: Next set of sales data starts at 25- Followed by hit/run columns starting at 29, 32, 35, 38 respectively
Your raw data has inconsistent spacing that breaks this alignment—for example, the third line Gilroy Garlics 151035 squishes two numeric fields together because there aren't enough spaces after the team name to push values to their correct columns.
Step 1: Adjust Raw Data at Column 19
To make your data fit the column positions you specified, modify the Onions.dat file to ensure:
- Every team name (including trailing spaces) fills exactly columns 1-20. For shorter team names, add spaces starting at column 19 to pad them out to 20 columns.
- Numeric values start precisely at their assigned column positions.
Here's the corrected data file:
Columbia Peaches 35 67 1 10 2 1 Plains Peanuts 210 . 2 5 0 2 Gilroy Garlics 151 35 12 11 7 6 Sacramento Tomatoes 124 85 15 4 9 1
- For
Columbia Peaches(16 characters total), we added 4 spaces starting at column 17 to fill up to column 20. - For
Gilroy Garlics(14 characters), we added 6 spaces starting at column 15 to reach column 20, which separates the team name from the151(ConcessionSales) starting at column 21.
Step 2: Run Your Original SAS Code
With the corrected data file, your existing SAS code will work as intended:
DATA sales; INFILE 'D:\Users\...\Desktop\Onions.dat'; INPUT VisitingTeam $ 1-20 ConcessionSales 21-24 BleacherSales 25-28 OurHits 29-31 TheirHits 32-34 OurRuns 35-37 TheirRuns 38-40; PROC PRINT DATA = sales; TITLE 'SAS Data Set Sales'; RUN;
You'll see all variables populated correctly, no missing values (except the intentional . for Plains Peanuts' BleacherSales), and no misaligned numeric data.
Alternative: Tweak the INPUT Statement (If You Can't Modify Data)
If you can't edit the data file, you can adjust the INPUT columns to match your actual data spacing. For example, if your team names only need up to 18 columns, shift the numeric fields left:
INPUT VisitingTeam $ 1-18 ConcessionSales 19-22 BleacherSales 23-26 OurHits 27-29 TheirHits 30-32 OurRuns 33-35 TheirRuns 36-38;
But fixing the data alignment is the cleaner, more maintainable long-term solution.
内容的提问来源于stack exchange,提问作者Mary

