BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Traian
Fluorite | Level 6

I have a dataset similar to this:

 

ID    QUESTION    OBJECTS    ANSWER

23    E1                                      1

23    E2                                      2

23    E4                  5                  1

23    E4                  2                  2

24    E1                                      2

24    E4                  1                  1

24    E4                  4                  2                                 

25    E1                                      2

25    E2                                      3

25    E4                  3                  3

25    E5                                      3

...

 

This set is based on people's answers to a poll. For example E1 stands for what is your gender. Then you have the value ANSWER 1 for male and 2 for female. Then we have E4 that stands for "How often do you eat different type of chocolate?".  The ANSWER value will denote the type of chocolate and OBJECTS will denote the frequency. What i am trying to do is to transpose the set but i am having trouble with the double values.

This is what i am trying to achieve.

IDc  E1    E2    E3    E4_1    E4_2    E5

23   1       2               5          1

23   1       2               2          2 

24   2                        1          1

24   2                        4          2          3

25   2      3                3          3          3

...

 

As you can see the row E4 has been split in E4_1 and E4_2 because of the double values in OBJECTS and ANSWER. This is what i have trouble replicating.

 

PROC TRANSPOSE data = Choc_pol out = test let;
by IDc;
id QUESTION;
var OBJECTS ANSWER;
run;

 

This will obviously not work because i have to split the E4_1 and E4_2 rows first to properly assign the data.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

something like this?

data have;
input ID    QUESTION $    OBJECTS    ANSWER;
cards;
23 E1 .  1
23 E2 .  2
23 E4 5 1
23 E4 2 2
24 E1 .  2
24 E4 1 1
24 E4 4 2
25 E1 .  2
25 E2 .  3
25 E4 3 3
25 E5 .  3
;
run;
proc print;
run;

proc sort data = have out = have2;
  by ID QUESTION;
run;
proc print;
run;

data have3;
  set have2;
  by ID QUESTION;
  if first.QUESTION then cnt = 0;
                    else cnt + 1;
run;
proc print;
run;

proc transpose data = have3 out = want DELIMITER=_;
by ID;
id QUESTION cnt;
var OBJECTS ANSWER;
run;
proc print;
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



View solution in original post

1 REPLY 1
yabwon
Onyx | Level 15

something like this?

data have;
input ID    QUESTION $    OBJECTS    ANSWER;
cards;
23 E1 .  1
23 E2 .  2
23 E4 5 1
23 E4 2 2
24 E1 .  2
24 E4 1 1
24 E4 4 2
25 E1 .  2
25 E2 .  3
25 E4 3 3
25 E5 .  3
;
run;
proc print;
run;

proc sort data = have out = have2;
  by ID QUESTION;
run;
proc print;
run;

data have3;
  set have2;
  by ID QUESTION;
  if first.QUESTION then cnt = 0;
                    else cnt + 1;
run;
proc print;
run;

proc transpose data = have3 out = want DELIMITER=_;
by ID;
id QUESTION cnt;
var OBJECTS ANSWER;
run;
proc print;
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



SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 526 views
  • 0 likes
  • 2 in conversation