BookmarkSubscribeRSS Feed
polpel
Fluorite | Level 6

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...

6 REPLIES 6
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



polpel
Fluorite | Level 6

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).

yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



polpel
Fluorite | Level 6

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)

yabwon
Amethyst | Level 16

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



polpel
Fluorite | Level 6
btw your thing works, and thanks a lot, but it displays all these "M F C" lines
thanks

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1186 views
  • 0 likes
  • 2 in conversation