Hi,
I would appreciate if someone could help me solve this problem with SAS.
From dataset t1 below, for each id_job, I need to calculate the total duration of years of exposure (dur_yrs) for each agent, a1_exp and a2_exp. I have several others, a3_exp, a4_exp……….. I am just using these two as tests.
The 1’s are exposures and the blanks are un-exposures.
The dataset, SAS code and log are found below: SAS output are attached.
data t1;
input id$ 1-6 id_job$ 7-13 dur_yrs 17-19 a1_exp 21-22 a2_exp 24-25;
datalines;
osa23 osa23_1 7 1
osb17 osb17_1 46 1 1
osb33 osb33_1 16 1
osb33 osb33_2 16 1
osb44 osb44_1 3 1
osb50 osb50_2 4 1 1
osb50 osb50_2 3 1 1
osb67 osb67_2 46 1 1
osb68 osb68_2 18
osb68 osb68_3 18 1
osb68 osb68_3 30 1
osb73 osb73_2 11 1
osc16 osc16_2 4 1
osc50 osc50_2 14 1 1
osc50 osc50_3 14 1 1
;
proc freq data=t1;
tables
a1_exp*dur_yrs
a2_exp*dur_yrs;
run;
I am finding total years of exposure for a1_exp: i.e. 7+46+3+4+3+46+11+4+14+14 =152 years of exposure. I used proc. freq but it could not compute the overall total exposure for me for a1_exp and a2_exp.
I want to create a new variable for the totals for a1_exp(a1_durtotal) and a2_exp(a2_durtotal) and print them out in a table: ie. id id_job……. a1_exp a1_durtotal a2_exp a2_durtotal.
Thanks in advance for your expert help.
proc
proc sql; select sum(dur_yrs) as a1_durtotal from t1 where a1_exp=1; select sum(dur_yrs) as a2_durtotal from t1 where a2_exp=1; quit; data test; set t1; if a1_exp=1 then a1_durtotal+dur_yrs; if a2_exp=1 then a2_durtotal+dur_yrs; put a1_durtotal= a2_durtotal=; run;
freq has no sum function, you can use data step or proc sql to get total number
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.