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

Hello all,

 

Running SAS 9.4. I have a table that looks like

 

Subject

Product

resp

4

B

3

7

A

1

9

B

3

25

C

4

 

data have;
input subject product $ resp;
datalines;
4 B 3
7 A 1
9 B 3
25 C 4
;

 

and would like it to look like this:

 

Product

Subject4

Subject7

Subject9

Subject25

A

.

1

.

.

B

3

.

3

.

C

.

.

.

4

 

I was able to accomplish this via some bulky code using arrays and proc transpose but am wondering if anyone has an elegant solution to this.

 

Thank you all.

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11
data have;
input Subject Product $ resp;
cards;
4 B 3
7 A 1
9 B 3
25 C 4
;
run;
 proc sort data =have;
 by product ;
 run;
proc transpose data =have out=want(drop=_name_)      prefix=Subject;
by product;
id subject;
run;

View solution in original post

3 REPLIES 3
mohamed_zaki
Barite | Level 11
data have;
input Subject Product $ resp;
cards;
4 B 3
7 A 1
9 B 3
25 C 4
;
run;
 proc sort data =have;
 by product ;
 run;
proc transpose data =have out=want(drop=_name_)      prefix=Subject;
by product;
id subject;
run;
Steelers_In_DC
Barite | Level 11

Here you go:

 

data have;
input subject product $ resp;
datalines;
4 B 3
7 A 1
9 B 3
25 C 4
;run;

proc sort data=have;by product;

proc transpose data=have out=want (drop=_NAME_) prefix=Subject;by product;id subject;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You have an answer, however is the output you post the best way forward?  I mean, how are you going to know what the variables are called, there doesn't appear to be a logical assignment.  Why not, if you have to transpose the data at all, creat the variables as a generic, then assign labels to the generic variables, this will make further processing far easier for you:

proc transpose data=have out=want;
  by product;
  var resp;
  label=subject;
run;

What you will get is a dataset, something like:

PRODUCT   VAR1   VAR2   VAR3  VAR4

...

With VAR1 label as _4, VAR2 as _7 etc.

 

Then in your programming you can refer to var1-var4, or as an array of var{4}.  None of which you can use if you don't know the variables.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 942 views
  • 1 like
  • 4 in conversation