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'm trying to transpose a dataset in the following way.

 

Input:

id       var      item      value

2        q1       .           1

4        q2       2          3

7        q2       5          3

8        q2       6          4

10      q3       1          2

23      q4       .           4

24      r1        .           5

 
Desired output:
id   q1    q2_2    q2_5    q2_6    q3_1    q4    r1
2    1      .           .           .           .           .       .
4     .      3          .           .           .           .       .
7     .      .           3          .           .           .       .
8     .      .           .           4          .           .       .
10   .      .           .           .           2          .       .
23   .      .           .           .           .           4      .
24   .      .           .           .           .           .       5
 
I tried this approach.
data have;
input id    var $    item    value;
cards;
2 q1 . 1
4 q2 2 3
7 q2 5 3
8 q2 6 4
10 q3 1 2
23 q4 . 4
24 r1 . 5
;
run;


PROC SORT data = have;
BY id var;
run; 

PROC TRANSPOSE data = have out = want delimiter=_;
by id;
id var item;
var value;
run;
The issue i have here is that i cannot transpose the variables where ITEM is an empty variable. Compared to the desired output i receive a table without q1, q4 and r1. How can i deal with the empty variables when transposing?
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Don't know what you want to do with the transposed format, but here you go:

data pre_want;
   set have;
   length key $ 4; /* needs adjustment */
   key = ifc(missing(item), ' ', put(item, 1.));
   key = catx('_', var, key);
   drop var item;
run;

proc transpose data=pre_want out=want;
   by key;
   id id;
   var value;
run;

You need to concatenate "var"  and "item" before proc transpose.

View solution in original post

1 REPLY 1
andreas_lds
Jade | Level 19

Don't know what you want to do with the transposed format, but here you go:

data pre_want;
   set have;
   length key $ 4; /* needs adjustment */
   key = ifc(missing(item), ' ', put(item, 1.));
   key = catx('_', var, key);
   drop var item;
run;

proc transpose data=pre_want out=want;
   by key;
   id id;
   var value;
run;

You need to concatenate "var"  and "item" before proc transpose.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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