**Assumptions:** I did not run SAS. I read only the text given. I have not seen the SAP, define.xml, setup.sas, or the SDTM/ADSL specs. Anything that depends on them is flagged.

## OBJECTIVE

**1. Hard-coded path outside the project (line 141)**
`ods rtf file="C:\Documents and Settings\kam39347\My Documents\CDISC Pilot\SDTM\ADAE.rtf";`
This is a personal user path. It will not exist for anyone else. It sits after `endsas;` (line 140), so it never runs. It is dead code.

**2. Dead code after `endsas;` (lines 140-144)**
`endsas;` is followed by `ods rtf`, `proc contents data=AE varnum;` and `ods rtf close;`. None of it runs. Either remove it or move it into a separate QC program.

**3. Relative include with an unseen dependency (line 5)**
`%include "..\setup.sas";`
The file cannot reproduce on its own. The path depends on the working directory. The libnames `sdtmnew` and `adamnew` (lines 7, 19, 134) must come from setup.sas, which I have not seen. Windows backslashes also make it non-portable.

**4. Commented-out and unused code (many lines)**
- Line 28: `/*and input(strip(substr(AESTDTC,1,4)),?? best.)=year(TRTSDT)*/`
- Lines 38-39: `*IF .z<TRTSDT<=ASTDT<=TRTEDT then ONTRTFL='Y';` and `*ELSE ONTRTFL='N';`
- Lines 49-53: the commented-out `proc freq`. It refers to `ATRTEMFL`, which is never created.
- Lines 84-86: the old `*%flagfrst(...SAEFL...)` calls.
- Lines 98-99 and 104-106: commented label entries.
- Line 110: `/*ANLSTDY = 'Analysis Start Day'*/`
- Lines 128-131: commented keep items such as `/*SUBJID*/`, `/*AESPID*/` and `/*ANLSTDY ONTRTFL*/`.

A cold reader cannot tell what was dropped on purpose and what was left by accident. The commented-out `ONTRTFL` is a question in itself. The final dataset has no on-treatment flag. Check this against the SAP.

**5. ANLSTDY is computed but not kept (line 37, lines 128-131)**
`IF ASTDT>.z and TRTSDT>.z then ANLSTDY=ASTDT-TRTSDT+1;`
`ANLSTDY` is computed, labelled only in a comment (line 110), and left out of the keep list. It also differs from `ASTDY` (lines 33-34), which has no day 0. Having both is confusing. If it is unused, delete it.

**6. Header comment is thin and misleading (lines 1-4)**
`** pch, 1/22/12 -- added amendment 1 coding vars;`
- The header gives no program name, study, purpose, inputs, outputs, or author and date of creation. It has only one change entry.
- "Amendment 1 coding vars" is not defined. A reader cannot tell which variables it means.
- Line 1 opens `*---...` and has no `;` until line 4, which works. The `**` on lines 2-3 sits inside that comment and is harmless.

**7. Derived-variable logic with no comments (lines 42-47)**
```
IF index(aedecod,'APPLICATION') or
...
(aebodsys='SKIN AND SUBCUTANEOUS TISSUE DISORDERS' and aedecod not in ('COLD SWEAT','HYPERHIDROSIS','ALOPECIA'))
then  CQ01NAM='DERMATOLOGIC EVENTS';
```
- The query definition is hard-coded as text strings. No comment gives its source (SAP, a named MedDRA query, or the amendment). The inclusion list and the three exclusions have no stated rationale.
- This is a patch trial, so application-site events are expected. Matching `'APPLICATION'` in `aedecod` is a text match, not a MedDRA code match. It may capture terms that are not application-site reactions. It could also miss any that are spelled differently. Case and MedDRA version matter here (see 11).
- `CQ01NAM` is set only for matches. It is never set to blank otherwise, which is fine, but the label (line 115) is only 'Customized Query 01 Name'.
- There is no `CQ01` flag or code variable, and no check that the exclusions match the real SOC wording.

**8. Hard-coded values**
- Line 13 and line 14: `ASTDT=input(strip(AESTDTC),?? yymmdd10.);` and `AENDT=input(strip(AEENDTC),?? yymmdd10.);`
- Line 28: `length(AESTDTC)=7`, with the `'-01'` append on line 29: `ASTDT=input(compress(AESTDTC||'-01'), yymmdd10.);`
- Line 30: `ASTDTF='D';`
- Line 32: `length CQ01NAM $25;`

These are fixed assumptions. Details are under PREFERENCE and POSSIBLE ISSUES below.

**9. Labels are missing or inconsistent (lines 98-121)**
- `TRTSDT`, `TRTEDT`, `TRTEMFL` and others: `TRTSDT` and `TRTEDT` have no label here. They may inherit one from ADSL. That is unseen.
- Line 100: `TRTEMFL = 'Treatment Emergent Analysis Flag'`.
- Lines 116-119: `AOCC02FL`..`AOCC04FL` are labelled "Serious" and `AOCC01FL` is labelled "CQ01". The numbering is the reverse of the logical order. The dermatologic flag is 01 yet is created last (line 90), and the serious flags are 02-04.
- Line 117: `AOCC03FL='1st Occurrence 03 Flag for Serious SOC'`.
- Lines 105-106 contain misspellings ("Ocurrence"), but they sit inside a comment.
- `ASTDT`..`AENDY` labels are fine.

**10. Variable-name checks that depend on unseen specs**
`TRTA` and `TRTAN` are renamed from `TRT01A` and `TRT01AN` (line 20). Whether ADAM requires `TRTA`/`TRTAN` is a spec question. I cannot confirm from this file. The same applies to the dataset label on line 134 and the variable set.

## POSSIBLE ISSUES, NEEDS A DATA CHECK

**A. Partial-date imputation (lines 28-31).**
`if ASTDT=. and length(AESTDTC)=7 ... then do; ASTDT=input(compress(AESTDTC||'-01'), yymmdd10.); ASTDTF='D'; end;`
- Only the `YYYY-MM` form is handled. A start date of only `YYYY`, length 4, stays missing. Those events then get `TRTEMFL='N'` (see C).
- The flag value `'D'` for a month-only date is a convention choice (ADaM uses 'D' for day imputed), but the imputation rule is not documented in the file.
- Imputing to the 1st of the month can set an AE before treatment start when the true date was after it. Example: `AESTDTC='2012-03'` and `TRTSDT=15MAR2012` gives `ASTDT=01MAR2012`, which is before TRTSDT. That event would be flagged `TRTEMFL='N'` even though the true date may be on or after treatment start. The commented-out year check on line 28 suggests this was discussed.
- Data check: list `AESTDTC` with `length<10` and compare against `TRTSDT` month. Confirm the rule in the SAP.

**B. `AENDT` has no partial-date handling.**
Line 14 parses `AEENDTC` with `??`, so any partial or invalid string silently becomes missing. `ADUR` (line 15) is then missing without notice.
Data check: count records where `AEENDTC` is non-blank and `AENDT` is missing.

**C. `TRTEMFL` logic (lines 40-41).**
`IF (ASTDT>=TRTSDT>.z) then TRTEMFL='Y'; ELSE TRTEMFL='N';`
- If `ASTDT` is missing, the condition is false and the flag is `'N'`. A missing start date is therefore treated as not treatment-emergent. Many SAPs treat an unknown start as emergent. Needs the SAP.
- There is no upper bound using `TRTEDT` (the commented ONTRTFL on line 38 had one). Events after the end of treatment plus any follow-up window count as emergent. Check against the SAP definition.
- If `TRTSDT` is missing (for example a subject not treated), `TRTEMFL='N'`. Data check: count records with missing `TRTSDT`.

**D. Mixed numeric comparison chain (lines 33-36).**
`IF ASTDT>=TRTSDT>.z then ...` is valid SAS and evaluates as two comparisons, so I do not call it an error. The `ELSE IF TRTSDT>ASTDT>.z` branch gives a negative day with no day 0, which is the standard ADaM convention. This works only if both dates are non-missing. No `ELSE` is needed there.

**E. Merge with ADSL (lines 24-27).**
`merge AE(in=inAE) TEMP; by USUBJID; if inAE;`
- A subject in AE but not in ADSL would keep missing `TRT`, `TRTSDT` and so on. No check or message flags this.
- The AE file has already been sorted by `USUBJID AESEQ`. ADSL is sorted by `USUBJID` in the same step. If ADSL had duplicate `USUBJID`, the merge would give a many-to-many warning and wrong carry-down. Data check: `proc sort nodupkey` on ADSL and compare counts.
- AEs are also not subset to the safety population. `SAFFL` is carried but not applied. Check against the SAP.

**F. `flagfrst` macro (lines 54-76).**
- Line 59: `%if &invar ne %then and &invar ne '';` The `where` clause is built from macro text. With `invar=aebodsys` it becomes `and aebodsys ne ''`, which drops records with a blank SOC or term. Those events then can never get the flag. Data check: any blank `AEBODSYS` or `AEDECOD`.
- Line 57 sorts `by usubjid &sortvars ASTDT`. With ties on `ASTDT` (including missing, excluded by `trtemfl='Y'`), which record is first depends on the sort order of the input. Two AEs on the same date could be chosen arbitrarily. `AESEQ` is not in the sort key, so the result may differ between runs. Data check: count subject/term groups with more than one record on the earliest date.
- Line 68: `if %if &invar= %then first.usubjid; %else first.&invar; then do ;` is hard to read. It works for `first.aebodsys` and `first.aedecod`.
- For `aedecod` (line 82), `sortvars=aebodsys aedecod` and `invar=aedecod`, first.aedecod within `usubjid aebodsys aedecod` is correct only if each term sits under one SOC. In the multi-SOC case, a PT that appears under two SOCs would get a first-flag for each. That may be intended. Check the SAP.
- Lines 87-89: the `where` includes `trtemfl='Y'` and `aeser='Y'`. Line 90's call for the dermatologic flag passes `ser=,derm=Y`, so `&ser` is blank and works.
- The macro has no comment block describing its parameters. The `%*` comment on line 78 is the only note: `%* Create first ae flag;`. It does not describe the serious or dermatologic calls.
- Macro-call argument names such as `outds=saea` (line 87) reuse the names of the older commented-out calls (lines 84-86). The output datasets `AEA`..`DERM` are only created if the calls run.

**G. Merge by `usubjid aeseq` (lines 95-97).**
`merge AE AEA AEB AEC SAEA SAEB SAEC DERM; by usubjid aeseq;`
This depends on `usubjid aeseq` being unique in `AE`. If not, the flags would be spread wrongly. Data check: `nodupkey` on `usubjid aeseq`. Flags that have no `Y` are left missing (not `N`), which is a common ADaM choice but should be confirmed.

**H. Final keep list vs retain list (lines 123-133).**
- Line 124-127 `retain` includes `AEREL`, `AEACN`, etc. in the order given. Variables in the `retain` statement that are not in the keep list get created as missing. Compare the two lists. For example, `AESOCCD`, `AELLTCD`, `AEPTCD`, `AEHLTCD` and `AEHLGTCD`: the keep uses wildcards `AELLT:`, `AEHL:`, `AESOC:`, so `AELLTCD`, `AEHLTCD` etc. are kept only if present in SDTM AE. If SDTM AE does not have them, they will come out as all-missing variables from the retain statement. Data check: `proc contents` on ADAE.
- `AEPTCD` is in both. `AEHLGT` and `AEHLGTCD` match `AEHL:`.
- `AESDTH`, `AESHOSP`, `AESLIFE` etc. depend on SDTM.
- `STUDYID` is retained and kept, but `STUDYID` is not in `TEMP` (it comes from AE), which is fine.
- `AESTDTC`, `AEENDTC`, `AESTDY`, `AEENDY` are intentionally dropped (commented in the keep list). The SDTM variable `AEENDY` is dropped, but a derived `AENDY` is kept. The name is easy to confuse with `AEENDY`. Fine, but confusing.
- `ASTDT` and `AENDT` formats: set at line 16 and again at line 137. The duplication is harmless.
- `ADUR` (line 15): `AENDT-ASTDT+1`. If `ASTDT` was imputed (month only), the duration is based on an imputed start. Undocumented.

**I. Sort order of the output.**
Line 123 data step sorts nothing. The input `AE` is sorted by `usubjid aeseq` so the output is. Line 132 `by usubjid aeseq;` would error if it were not sorted, so it acts as a check.

**J. Case of values.**
`aedecod`, `aebodsys` comparisons (lines 42-46) are case-sensitive. `index(aedecod,'APPLICATION')` will miss 'Application site pruritus' if the dictionary values are mixed case. Data check: run `proc freq` on `AEDECOD` and `AEBODSYS` to see the case.

## PREFERENCE

1. Mixed upper and lower case keywords and variable names (lines 33-36 `IF`/`ELSE IF`, lines 42-47, vs lowercase elsewhere). A reasonable programmer could accept it.
2. `length CQ01NAM $25;` (line 32) is placed in the middle of the step. Most would put it before first use. `'DERMATOLOGIC EVENTS'` is 19 characters, so $25 works, but it is a magic number.
3. Dataset names reuse `AE` for every step (lines 10, 24, 95, 123). That makes the log hard to follow and means rerunning a single step is not possible. Consider distinct names.
4. Macro parameter handling by `%if &invar ne %then` with empty values could use `%length` or `%sysevalf`; readability opinion.
5. `proc sort ... OUT=` and `out=` with inconsistent case.
6. The `keep` in the proc sort option on lines 19-20 is a long single line with the `RENAME` on the next line. Splitting it would help.
7. `format ASTDT AENDT DATE9.;` (line 16) and again at line 137 along with `TRTSDT TRTEDT`; keep one.
8. Line 140 `endsas;` as a bare statement is unusual in a delivered program.
9. The flag naming (`AOCC01FL`..`AOCC04FL`) and the order in which they are labelled and created do not match. Consider ordering by number.
10. Variable ordering depends on `retain` ahead of `set`, a known SAS trick. It works, but a `retain` list that is also the "order" statement is easy to misread. An `attrib` or a final `keep` order would be clearer.
11. Lines 2-3 comment style (`**`) differs from the `*---` style on line 1 and the `/* */` blocks elsewhere.

## Questions for the programmer

1. What is the source and the exact definition of `CQ01NAM` (lines 42-47)? Is it from the SAP, a named MedDRA query, or Amendment 1? Why those three exclusions?
2. What does the SAP say about treatment-emergent when `ASTDT` is missing or partial, and about an upper bound using `TRTEDT` or a follow-up window? Why was `ONTRTFL` removed?
3. How are `YYYY`-only start dates and partial end dates meant to be handled? Why impute to the 1st of the month, and is `'D'` the right `ASTDTF` value?
4. Is `ANLSTDY` needed? If not, remove it. If so, why is it not kept?
5. Which of the `AELLTCD`, `AEPTCD`, `AEHLTCD`, `AEHLGTCD`, `AESOCCD` variables actually exist in SDTM AE? Does the retain list create any all-missing columns in ADAE?
6. Are `USUBJID AESEQ` unique in SDTM AE and `USUBJID` unique in ADSL? Are there AE subjects not in ADSL? Is the safety population applied anywhere?
7. How are ties on `ASTDT` broken in the first-occurrence flags, and do any blank `AEBODSYS` or `AEDECOD` values exist?
8. Why are the flags numbered `AOCC01FL`..`AOCC04FL` in this order, and do they match define.xml and the shells? (I have not seen define.xml.)
9. What does `setup.sas` define, and can the program run from a clean session? Can the dead code after `endsas;`, including the personal path, be deleted?
10. Should the commented-out code and the stale header be cleaned up before submission, and should a full change history be added?