People with HIV have a substantially higher risk of anal cancer than the general population. We aimed to identify risk factors associated with the development of anal cancer among people with HIV to implement more effective and targeted screening strategies. We conducted a multicentre retrospective cohort study in 16 hospitals across Catalonia and the Balearic Islands, Spain, between Jan 1, 1998, and Dec 31, 2022. Treatment-naive people with HIV nested in the PISCIS cohort aged 16 years and older with biopsy-proven squamous cell carcinoma of the anus or anal canal were eligible for inclusion. Data were retrieved from every hospital registry and were centrally validated in the PISCIS cohort and the Public Data Analysis for Health Research and Innovation Program. The primary outcome was the incidence rate (IR) of histologically confirmed anal cancer. We used Poisson regression to examine the association between the following risk factors and incidence of anal cancer: age, mode of HIV transmission, nadir CD4 cell count, and time period of HIV diagnosis. Among 14 238 people with HIV, 107 (0·8%) developed anal cancer, with an overall IR of 72·5 cases per 100 000 person-years (95% CI 59·4-87·6) and median follow-up of 9·5 years (IQR 4·4-15·7). Of these patients with anal cancer, 37 (34·6%) died, of which 24 (64·9%) deaths were related to anal cancer. Incidence was highest among people with HIV with historical nadir CD4 counts of less than 200 cells per μL (IR 105·0 person-years, 95% CI 82·0-132·5) and lowest among those with counts of more than 350 cells per μL (2·9 person-years, 0·1-16·0). Among men who have sex with men (MSM), the IR was 211·5 person-years (95% CI 151·1-211·7) among those with a CD4 count of less than 200 cells per μL, 37·6 person-years (16·2-74·1) among those with a count of 200-350 cells per μL, and 4·8 person-years (0·1-26·9) among those with a count of more than 350 cells per μL. Among people with HIV younger than 30 years, there were no cases of anal cancer among women or men who do not have sex with men, and one case among MSM with a nadir CD4 count of more than 350 cells per μL (IR 4·8 person-years, 95% CI 0·1-26·9). In the multivariable analysis, people with HIV with nadir CD4 counts of more than 350 cells per μL had the lowest risk of developing anal cancer, compared with people with HIV with counts of less than 200 cells per μL (adjusted IR ratio 0·03, 95% CI 0·00-0·25; p=0·0010) or 200-350 cells per μL (0·30, 0·17-0·55; p<0·0001). Compared with people with HIV younger than 30 years, people with HIV aged 60 years and older had an adjusted IR ratio of 27·6 (3·7-206·9; p=0·0010) and people with HIV aged 45-59 years of 21·6 (3·0-156·4; p=0·0020). Compared with individuals diagnosed after 2015, a diagnosis of HIV before 1998 had an adjusted IR ratio of 33·0 (7·9-137·5; p<0·0001). A nadir CD4 count threshold below 350 cells per μL, particularly less than 200 cells per μL, has the potential to identify people with HIV at heightened risk of developing anal cancer. Customised screening strategies that prioritise screening for individuals at high risk with this surrogate marker could maximise available resources. External validation of these data with other cohorts is required before screening recommendations can be updated. Catalan Health Department, Generalitat de Catalunya.