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

I want to collapse multiple columsn (as some are empty) into few columsn to reduce size of the file.  

 

Maximum columns populated in Have is 3 so poutput should have 3 columns and orignial 5 can be dropped

 

Have

Record             Class1               Class2             Class3           Class4          Class5

1                        A                                               C                 

 

2                                                   B

 

3                                                                                                D                    E

 

4                       A

 

5                                                      B                  C                   D

 

Want   

 

Record        Classnew1               ClassNew2             ClassNew3        

1                     A                                 C                 

2                     B

3                     D                                 E

4                     A

5                     B                                 C                            D

 

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

Hi, I'm sure this question will get lots of replies.  Here's on idea ...

 

data x;
input record (class1-class5) (:$1.);
datalines;
1 A . C . .
2 . B . . .
3 . . . D E
4 A . . . .
5 . B C D .
;

 

data y (keep=record class: );
set x;
array class(5);
new = catt(of class(*));
do i=1 to 5;
    class(i) = char(new,i);
end;
run;

 

data set Y ...

Obs    record    class1    class2    class3    class4    class5

 1        1        A         C
 2        2        B
 3        3        D         E
 4        4        A
 5        5        B         C         D

View solution in original post

2 REPLIES 2
MikeZdeb
Rhodochrosite | Level 12

Hi, I'm sure this question will get lots of replies.  Here's on idea ...

 

data x;
input record (class1-class5) (:$1.);
datalines;
1 A . C . .
2 . B . . .
3 . . . D E
4 A . . . .
5 . B C D .
;

 

data y (keep=record class: );
set x;
array class(5);
new = catt(of class(*));
do i=1 to 5;
    class(i) = char(new,i);
end;
run;

 

data set Y ...

Obs    record    class1    class2    class3    class4    class5

 1        1        A         C
 2        2        B
 3        3        D         E
 4        4        A
 5        5        B         C         D

LinusH
Tourmaline | Level 20
Does not the original column name or order have any meaning? How will you use the table further on?
I would sugest that you transpose the table instead, resulting in record, class_no and class_value columns. See PROC TRANSPOSE for details.
Data never sleeps

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 796 views
  • 1 like
  • 3 in conversation