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

Hi,

 

Would anyone know how to move data in columns to the left so that there's no missing cells in between them:

 

have:

Title Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9
Colors1 Red Blue Black   Yellow Green      
Colors2 Red     Purple Yellow     Orange Violet
Colors3 Red   Black   Yellow   White Orange Violet
Colors4 Red     Purple Yellow     Orange Violet

 

 

want:

Title Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9
Colors1 Red Blue Black Yellow Green        
Colors2 Red Purple Yellow Orange Violet        
Colors3 Red Black Yellow White Orange Violet      
Colors4 Red Purple Yellow Orange Violet        

 

thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Below one way how you could do this.

data have;
  infile datalines truncover dsd;
  input (Title Column1-Column9) ($);
  datalines;
Colors1,Red,Blue,Black,,Yellow,Green,,,
Colors2,Red,,,Purple,Yellow,,,Orange,Violet
Colors3,Red,,Black,,Yellow,,White,Orange,Violet
Colors4,Red,,,Purple,Yellow,,,Orange,Violet
;

data want;
  set have;
  array _cols {*} Column9 Column8 Column7 Column6 Column5 Column4 Column3 Column2 Column1 ;
  call sortc(of _cols[*]);
run;

 

Patrick_0-1660430434967.png

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

May I ask what you plan to do with this data? What is the next analysis or report that will be done once you remove these missing cells?


What is it you can do without the missing cells that you can't do with the missing cells present?

 

Please explain.

--
Paige Miller
arde
Obsidian | Level 7

I am creating a csv file.  the csv file is being used to create a display.  I have 10,000 rows.  The display doesn't look so nice because of all the gaps in between the data.

Kurt_Bremser
Super User

Take the opportunity and convert your dataset to a usable long structure.

proc transpose data=have out=want (where=(col1 ne ""));
by title;
var column:;
run;
arde
Obsidian | Level 7

will proc transpose work?  I have 10,000 rows in my main file

SASKiwi
PROC Star

There is no harm at all in just trying it as an experiment. Even if it fails it will likely move you closer to a solution.

Patrick
Opal | Level 21

Below one way how you could do this.

data have;
  infile datalines truncover dsd;
  input (Title Column1-Column9) ($);
  datalines;
Colors1,Red,Blue,Black,,Yellow,Green,,,
Colors2,Red,,,Purple,Yellow,,,Orange,Violet
Colors3,Red,,Black,,Yellow,,White,Orange,Violet
Colors4,Red,,,Purple,Yellow,,,Orange,Violet
;

data want;
  set have;
  array _cols {*} Column9 Column8 Column7 Column6 Column5 Column4 Column3 Column2 Column1 ;
  call sortc(of _cols[*]);
run;

 

Patrick_0-1660430434967.png

 

arde
Obsidian | Level 7
thank you so much
Ksharp
Super User
data have;
  infile datalines truncover dsd;
  input (Title Column1-Column9) ($);
  datalines;
Colors1,Red,Blue,Black,,Yellow,Green,,,
Colors2,Red,,,Purple,Yellow,,,Orange,Violet
Colors3,Red,,Black,,Yellow,,White,Orange,Violet
Colors4,Red,,,Purple,Yellow,,,Orange,Violet
;

data want;
  set have(rename=(column1-column9=_c1-_c9));
  array _cols {*} $ _c1-_c9 ;
  array x {*} $ 80 column1-column9 ;
  n=0;
  do i=1 to dim(_cols);
    if not missing(_cols{i}) then do;n+1;x{n}=_cols{i};end;
  end;
  keep Title Column1-Column9;
run;

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
  • 8 replies
  • 1365 views
  • 3 likes
  • 6 in conversation