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

I have a dataset in long format, example below: I need to convert it to one observation per person with the codes as the variables names and the value as the values for each person. So I would end up with 7 variables: studyid and S01-S06. Any help to do this would be appreciate! 

 

 

studyid

code value
27 S01 3.25
27 S02 2.75
27 S03 2.75
27 S04 2.75
27 S05 2.75
27 S06 2.75
28 S01 2.75
28 S02 2.75
28 S03 2.5
28 S04 2.75
28 S05 2.75
28 S06 2.75
29 S01 2
29 S02 2
29 S03 2.5
29 S04 2.5
29 S05 2.5
29 S06

2

1 ACCEPTED SOLUTION

Accepted Solutions
TarunKumar
Pyrite | Level 9

DATA HAVE;
INPUT Studyid code$3.value;
DATALINES;
27 S01 3.25
27 S02 2.75
27 S03 2.75
27 S04 2.75
27 S05 2.75
27 S06 2.75
28 S01 2.75
28 S02 2.75
28 S03 2.5
28 S04 2.75
28 S05 2.75
28 S06 2.75
29 S01 2
29 S02 2
29 S03 2.5
29 S04 2.5
29 S05 2.5
29 S06 2
;
RUN;


PROC SORT DATA= HAVE ;BY Studyid CODE;RUN;

 

PROC TRANSPOSE DATA=HAVE

OUT=WANT (DROP=_NAME_);
BY Studyid;

ID code;

VAR value;

RUN;

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

something like this

proc transpose data=have out=want(drop =_name_);
    by studyid ;
    id code;
    var value;
run;
Jagadishkatam
Amethyst | Level 16

alternatively please try arrays, but have to create a numeric variable num  before

 

data have;
input studyid 	code$ 	value;
num=input(compress(code,,'kd'),best.);
cards;
27 	S01 	3.25
27 	S02 	2.75
27 	S03 	2.75
27 	S04 	2.75
27 	S05 	2.75
27 	S06 	2.75
28 	S01 	2.75
28 	S02 	2.75
28 	S03 	2.5
28 	S04 	2.75
28 	S05 	2.75
28 	S06 	2.75
29 	S01 	2
29 	S02 	2
29 	S03 	2.5
29 	S04 	2.5
29 	S05 	2.5
29 	S06 	2
;

data want;
set have;
by studyid;
retain s01-s06;
array s(6) s01-s06;
if first.studyid then call missing(of s(*));
s(num)=value;
if last.studyid;
run;

 

 

Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

using arrays and without the num variable from code we could try

 

data want;
set have;
by studyid;
retain s01-s06;
array s(*) s01-s06;
if first.studyid then call missing(of s(*));
if first.studyid then i=1;
else i+1;
s(i)=value;
if last.studyid;
run;
Thanks,
Jag
TarunKumar
Pyrite | Level 9

DATA HAVE;
INPUT Studyid code$3.value;
DATALINES;
27 S01 3.25
27 S02 2.75
27 S03 2.75
27 S04 2.75
27 S05 2.75
27 S06 2.75
28 S01 2.75
28 S02 2.75
28 S03 2.5
28 S04 2.75
28 S05 2.75
28 S06 2.75
29 S01 2
29 S02 2
29 S03 2.5
29 S04 2.5
29 S05 2.5
29 S06 2
;
RUN;


PROC SORT DATA= HAVE ;BY Studyid CODE;RUN;

 

PROC TRANSPOSE DATA=HAVE

OUT=WANT (DROP=_NAME_);
BY Studyid;

ID code;

VAR value;

RUN;

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 5248 views
  • 0 likes
  • 4 in conversation