Good morning:
i have this data set:
Question | Sex | yes_no |
1 | male | 1 |
1 | male | 1 |
1 | male | 0 |
1 | male | 0 |
1 | male | 0 |
1 | male | 0 |
1 | male | 0 |
2 | male | 1 |
2 | male | 1 |
2 | male | 1 |
2 | male | 0 |
2 | male | 0 |
2 | male | 0 |
2 | male | 0 |
3 | male | 1 |
3 | male | 1 |
3 | male | 1 |
3 | male | 1 |
3 | male | 1 |
3 | male | 0 |
and i need to obtain this one, as follows:
Sex | Question_1 | Question_2 | Question_3 |
male | 1 | 1 | 1 |
male | 1 | 1 | 1 |
male | 0 | 1 | 1 |
male | 0 | 0 | 1 |
male | 0 | 0 | 1 |
male | 0 | 0 | 0 |
male | 0 | 0 | . |
Thanks in advance
Hello @jonatan_velarde,
Here is an example illustrating NicoM's suggestion:
proc transpose data=have out=trans(drop=_:);
by sex question;
var yes_no;
run;
proc transpose data=trans out=want(drop=_:) prefix=Question_;
by sex;
id question;
var col:;
run;
The first step assumes that HAVE is sorted by sex question, which is the case for your sample data.
Have you tried using PROC TRANSPOSE? You can accomplish what you want using that procedure. You can find some basic examples in this document: 060-2009: Learn the Basics of PROC TRANSPOSE (sas.com).
data have;
input question sex $ yes_no;
datalines;
1 male 1
1 male 1
1 male 0
1 male 0
1 male 0
1 male 0
1 male 0
2 male 1
2 male 1
2 male 1
2 male 0
2 male 0
2 male 0
2 male 0
3 male 1
3 male 1
3 male 1
3 male 1
3 male 1
3 male 0
;
data want;
if 0 then set have (keep=sex); /* sets attributes */
call missing(sex,question_1,question_2,question_3);
merge
have (
rename=(yes_no=question_1)
where=(question = 1)
)
have (
rename=(yes_no=question_2)
where=(question = 2)
)
have (
rename=(yes_no=question_3)
where=(question = 3)
)
;
drop question;
run;
Please note how example data is presented in a data step with datalines; this removes any doubts about content or variable attributes. Do so in the future. It's basic courtesy towards your helpers.
The table you want looks like a report and not like a dataset. Are you sure, that you need the wide structure for further processing?
Hello @jonatan_velarde,
Here is an example illustrating NicoM's suggestion:
proc transpose data=have out=trans(drop=_:);
by sex question;
var yes_no;
run;
proc transpose data=trans out=want(drop=_:) prefix=Question_;
by sex;
id question;
var col:;
run;
The first step assumes that HAVE is sorted by sex question, which is the case for your sample data.
If this is at all related to this thread https://communities.sas.com/t5/SAS-Programming/Convert-data-set-from-summary-to-full-data/m-p/728565 where you convert summary data into an arbitrary ordered set by categorical variables then the target set is invalid for any sort of analysis. You are creating an artificial association between the values of the question variables.
Really.
If you do not have the individual level data to start with then your entire approach is going to be so flawed I can't describe it.
There are some things that might be possible with something like Proc Mixed and similar procedures but your "created from summary to individual level data" is misrepresenting the information.
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.