Hello,
Basically what I have as a dataset is a table with different types of people (man, female, child) in families which have same ID (for example a family with parent + child, there would be the same ID).
What I need to produce is a frequency dataset, where I give the frequency of differently composed families (child + 2 parents, 2 parents + no child or 1 person without children). So the end table would have 2 lines a 3 cols, and the "have" dataset is a table with family id, and "gender" (m/f/child).
Thanks and hope it was clear enough haha.
I would post code but it really wouldnt be constructive as i really don't have any clue on how to do this...
Hi,
do you mean something like this:
data have;
input id person $ ;
cards;
1 man
1 female
1 child
2 man
2 female
2 child
3 man
3 female
3 child
4 man
4 female
4 child
5 man
5 child
6 man
6 child
6 female
6 child
6 child
7 female
7 child
8 female
8 child
9 female
9 child
9 child
9 child
;
run;
proc sort data = have;
by id descending person;
run;
data tmp;
set have;
by ID;
length type $ 20; retain type;
if first.id then type = " ";
type = catx(" ", type, char(person, 1));
if last.id then output;
run;
proc tabulate data = tmp;
class type;
table type, N;
run;
All the best
Bart
what i need at the end is something like this:
M + F + C | M+F no C | M, no F, no C | no M, F, no C
nb of fams with M+F+C | nb fams with M+F no C | nb fams with M no F no C | nb fams with F, but no M and no C
M being the male parent, F being the female parent and C being the child.
Each family has same ID (a number).
Something like this?
data have;
input id person $ ;
cards;
1 man
1 female
1 child
2 man
2 female
2 child
3 man
3 female
3 child
4 man
4 female
4 child
5 man
5 child
6 man
6 child
6 female
6 child
6 child
7 female
7 child
8 female
8 child
9 female
9 child
9 child
9 child
10 man
10 female
11 man
12 female
13 child
13 child
13 child
;
run;
proc sort data = have;
by id descending person;
run;
data tmp;
set have;
by ID;
length type $ 20; retain type;
if first.id then type = " ";
type = catx(" ", type, lowcase(char(person, 1)));
if last.id then
do;
type = catx(","
,ifc(index(type, "m"), "M", "no M")
,ifc(index(type, "f"), "F", "no F")
,ifc(index(type, "c"), "C", "no C")
);
output;
end;
run;
proc tabulate data = tmp;
class type;
table N, type;
run;
Bart
hello again and thanks for quick response!
i was able to do this, i'm almost finished i just need to set lines as cols and cols as lines.
what i did was set the max length of type to 5 so i would only get the composition as (M F C). then i made a format and applied it to the dataset and removed every row which contained errors (family with just child for example)
That last proc tabulate reverses cols and rows,
the first one was: "table type,N;" the newer one is "table N,type".
If you need "transposed" dataset then use proc transpose ( https://documentation.sas.com/?docsetId=proc&docsetTarget=p1r2tjnp8ewe3sn1acnpnrs3xbad.htm&docsetVer... 😞
proc tabulate data = tmp output = tmp2(keep = type N);
class type;
table N, type;
run;
proc transpose data = tmp2 out = tmp3;
var N;
id type;
run;
All the best
Bart
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.