Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions SAS Examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SAS Examples

This directory contains SAS programming examples and solutions to common issues.

## Data Input Alignment Fix (menages_corrected.sas)

### Problem
In the original code, the data values were not properly aligned, causing "karim" to start in the second column (Sexe) instead of the first column (Nom).

### Original Problematic Code
```sas
Data menages_init;
length Nom $ 10;
input Nom $ Sexe $ Revenu Epargne Anciennete Statut_familial $;
list;
Cards;
karim H 38.5 5.9 10 marie
fatima F 31.2 3.8 6 celib
...
;
Run;
```

### Issues Identified
1. **The `list;` statement**: This was incorrectly placed and not terminated properly
2. **Indentation before data**: The data lines had leading spaces/tabs, which can cause misalignment
3. **`Cards;` vs `Datalines;`**: While both work, `Datalines;` is the modern SAS syntax

### Solution
The corrected code removes the `list;` statement, uses `Datalines;`, and ensures all data starts at the beginning of each line without leading spaces:

```sas
Data menages_init;
length Nom $ 10;
input Nom $ Sexe $ Revenu Epargne Anciennete Statut_familial $;
Datalines;
karim H 38.5 5.9 10 marie
fatima F 31.2 3.8 6 celib
...
;
Run;
```

### Key Points for List Input in SAS
- **List input** (default when using `$` and no column specifications) reads space-delimited values
- Data should start at the beginning of lines to avoid parsing issues
- Each value is read in order based on the INPUT statement
- Character variables need the `$` modifier
- No leading spaces should precede the first value on each data line

### Expected Output
The dataset `menages_init` will be created with 20 observations and the following variables:
- **Nom**: Name (character, length 10)
- **Sexe**: Gender (character)
- **Revenu**: Revenue (numeric)
- **Epargne**: Savings (numeric)
- **Anciennete**: Seniority (numeric)
- **Statut_familial**: Marital status (character)

All values will be correctly aligned in their respective columns.
44 changes: 44 additions & 0 deletions SAS Examples/menages_corrected.sas
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Corrected SAS Code - Fixed Data Input Alignment Issue */
/*
Problem: The data was not properly aligned, causing values to shift into wrong columns.
Solution: Properly format the data with consistent spacing and ensure each value
is in its correct position for list input.
*/

Data menages_init;
length Nom $ 10;
input Nom $ Sexe $ Revenu Epargne Anciennete Statut_familial $;
Datalines;
karim H 38.5 5.9 10 marie
fatima F 31.2 3.8 6 celib
nassim H 27.8 2.0 4 celib
amina2 F 35.6 5.2 8 marie
tarek H 29.9 1.5 3 celib
leila F 41.3 6.7 12 marie
samir H 50.1 10.0 18 divorce
nora F 24.5 2.2 2 celib
mounir H 33.9 4.1 7 celib
luc H 42.5 5.2 12 marie
amina F 28.3 3.1 5 celib
paul H 56.7 12.4 20 marie
sarah F 37.9 7.0 10 divorce
jean H 22.4 1.2 2 celib
claire F 49.8 15.3 25 marie
youssef H 33.1 4.8 8 celib
marion F 25.6 2.5 3 celib
mohamed H 61.2 18.1 30 marie
lea F 20.3 0.7 1 celib
julien H 45.0 6.5 15 divorce
;
Run;

/*
KEY CHANGES MADE:
1. Removed the "list;" statement that was causing issues
2. Changed "Cards;" to "Datalines;" (more modern SAS syntax)
3. Ensured data starts from the beginning of each line (no leading spaces before names)
4. All values are properly space-delimited for list input mode

With list input (default when using $), SAS reads values separated by spaces.
The data should start at the beginning of the line to ensure proper alignment.
*/