s**f 发帖数: 365 | 1 So sorry I can't type Chinese now. Please help me with a SAS question. I am
still learning the basics of SAS, please bear with me. thank you!
There is a dataset below, how can I calculate the sum of "population" for
age 24-44, 49-64, and 69-85 for each year in SAS?
Year age population
1981 4 ***
1981 9 ***
1981 14 ***
1981 19 ***
1981 24 ***
1981 29 ***
1981 34 ***
1981 39 ***
1981 44 ***
1981 49 ***
1981 54 ***
1981 59 ***
1981 64 ***
1981 69 ***
1981 74 ***
1981 79 ***
1981 84 ***
1981 85 ***
1982 4 ***
1982 9 ***
1982 14 ***
1982 19 ***
1982 24 ***
...
...
...
How can I generate a new dataset contains the following information?
Year age population
1981 24-44 ###
1981 49-64 ###
1981 69-85 ###
1982 24-44 ###
1982 49-64 ###
1982 69-85 ###
1983 24-44 ###
1983 49-64 ###
1983 69-85 ###
...
... |
p********a 发帖数: 5352 | 2 select year,"24-44" as age_cat, sum(population) as population from dst where
24<=age<=44 group by year
union
select year,"49-64" as age_cat, sum(population) as population from dst where
49<=age<=64 group by year
union
select year,"69-85" as age_cat, sum(population) as population from dst where
69<=age<=85 group by year |
h********o 发帖数: 103 | 3 proc sql;
select year,"24-44" as age,sum(population) as population
from one
where age between 24 and 44
group by year
union
select year,"49-64" as ageRange,sum(population) as total
from one
where age between 49 and 64
group by year
union
select year,"69-64" as ageRange,sum(population) as total
from one
where age between 69 and 85
group by year
order by year;
quit; |
h********o 发帖数: 103 | 4 Sorry, The "ageRange" should be changed to "age" |
s**f 发帖数: 365 | 5 Thank you for replying, doesn't look like SAS entry level coding ah.
I am still at DATA ###; PROC ###; RUN; level.
where
where
where
【在 p********a 的大作中提到】 : select year,"24-44" as age_cat, sum(population) as population from dst where : 24<=age<=44 group by year : union : select year,"49-64" as age_cat, sum(population) as population from dst where : 49<=age<=64 group by year : union : select year,"69-85" as age_cat, sum(population) as population from dst where : 69<=age<=85 group by year
|
s**f 发帖数: 365 | 6 Thank you for replyin! I know nothing about SQL, need to read more.
I am still at DATA ###; PROC ###; RUN; level.
【在 h********o 的大作中提到】 : proc sql; : select year,"24-44" as age,sum(population) as population : from one : where age between 24 and 44 : group by year : union : select year,"49-64" as ageRange,sum(population) as total : from one : where age between 49 and 64 : group by year
|
h********o 发帖数: 103 | 7 For the entry level,you can do it like this:
data one two three;
set orig;
if 24 <= age <= 44 then output one;
if 49 <= age <= 64 then output two;
if 69 <= age <= 85 then output three;
run;
proc sort data = one;
by year;
run;
proc sort data = two;
by year;
run;
proc sort data = three;
by year;
run;
data one;
set one;
drop age;
ageRange = "24-44";
by year;
if first.year then sum = 0;
sum + population;
if last.year;
run;
data two;
set two;
drop age;
ageRange = "49-64";
by year;
if first.year then sum = 0;
sum + population;
if last.year;
run;
data three;
set three;
drop age;
ageRange = "69-85";
by year;
if first.year then sum = 0;
sum + population;
if last.year;
run;
data final(rename = (sum = population
ageRange = age));
set one two three;
drop population;
run;
proc sort data = final;
by year;
run; |
s**f 发帖数: 365 | 8 Hi! I am looking the SAS documentation for SQL now. I definitely need SQL.
Thank you for the entry level codes!
【在 h********o 的大作中提到】 : For the entry level,you can do it like this: : data one two three; : set orig; : if 24 <= age <= 44 then output one; : if 49 <= age <= 64 then output two; : if 69 <= age <= 85 then output three; : run; : proc sort data = one; : by year; : run;
|
o****o 发帖数: 8077 | 9 no one even ever mentioned FORMAT, i.e. formated class variable? |
j******o 发帖数: 127 | 10 Format should be more efficient.
【在 o****o 的大作中提到】 : no one even ever mentioned FORMAT, i.e. formated class variable?
|
b*****e 发帖数: 223 | 11 用 proc format 重新 code: age 24-44, 49-64, and 69-85 -》 产生一个新 var
然后用 proc freq 对 Year 和这个 新 var 求和 |
s**f 发帖数: 365 | 12 Hao!
【在 b*****e 的大作中提到】 : 用 proc format 重新 code: age 24-44, 49-64, and 69-85 -》 产生一个新 var : 然后用 proc freq 对 Year 和这个 新 var 求和
|
s******r 发帖数: 1524 | 13 efficient in coding, inefficient in running.
【在 j******o 的大作中提到】 : Format should be more efficient.
|