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

Good day! My data looks like this:

ID      B1 B2 B3

1        1   2   3

2        4   5   6             

3        7   8   9

 

I would like it to look like this:

ID    B# Content

1       1      1

1       2      2

1       3      3

2       1      4

2       2      5

2       3      6

3       1      7

3       2      8

3       4      9

 

Could you pls help me. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

In addition to transposing, you could do it in a data step. I assume your last row is a typo, though. I don't know where the b# = 4 would come from.

 

data have;
    input ID B1 B2 B3;
datalines;
1 1   2   3
2 4   5   6
3 7   8   9
;

data want(drop = b:);
    set have;
    array b{3};
    do num_b = 1 to 3;
        content = b(num_b);
        output;
    end;
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Without test data in the form of a datastep, this is just a guess:

proc transpose data=have out=want;
  by id;
  var b:;
run;
collinelliot
Barite | Level 11

In addition to transposing, you could do it in a data step. I assume your last row is a typo, though. I don't know where the b# = 4 would come from.

 

data have;
    input ID B1 B2 B3;
datalines;
1 1   2   3
2 4   5   6
3 7   8   9
;

data want(drop = b:);
    set have;
    array b{3};
    do num_b = 1 to 3;
        content = b(num_b);
        output;
    end;
run;
cdvdc
Calcite | Level 5

thank you so much! the last one was a typo 

collinelliot
Barite | Level 11

You're welcome. @RW9' s solution would probably work for you, too, and you should probably get familiar with how proc transpose works since there are times when it will be far easier than a data step approach. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 873 views
  • 3 likes
  • 3 in conversation