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

Hello friends,

I want to ask a question please.

I have a data set which includes 4 rows with 2 columns.

X   Y

1   1

2   2

2   3

2   4

I want to create following data set from my data set

X     Y

1     1

2      2,3,4

Can anyone help how to do it?

 

Thanks

Ronein

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

This is simple by-group-processing, explained in numerous papers & posts.

 

Untested, assuming that work.want is sorted by x:

data work.want;
   set work.have;
   by x;

   length Y_List $ 100;
   retain Y_List;

   if first.x then Y_List = ' ';
   Y_List = catx(',', Y_List, Y);
   if last.x then output; 

   drop y;
run;

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

This is simple by-group-processing, explained in numerous papers & posts.

 

Untested, assuming that work.want is sorted by x:

data work.want;
   set work.have;
   by x;

   length Y_List $ 100;
   retain Y_List;

   if first.x then Y_List = ' ';
   Y_List = catx(',', Y_List, Y);
   if last.x then output; 

   drop y;
run;
Ronein
Meteorite | Level 14

It is a perfect and very clever solution.

May you please explain the code and steps of process.

I am trying to understand it but couldn't understand.

Thank you so much!

 

ballardw
Super User
data work.want;
   set work.have;
   by x;  /* assumes data is sorted by this variable
             automatic variables for variables on the 
             by statement are available to indicate the 
             first or last of a group represented by 
             the variable(s)
           */

   length Y_List $ 100; /* specify a length of the target variable
                           long enough to hold the longest expected
                           result
                        */
   retain Y_List;       /* keep the value of the variable between iterations
                           of the data step
                        */

   if first.x then Y_List = ' ';  /* set the target variable to empty at 
                                     the start of each group of X values
                                  */
   Y_List = catx(',', Y_List, Y); /* catx function combines the previous version of the
                                     Y_list variable with the current inserting a comma
                                     note the comma only appears BETWEEN values, not after
                                     the last
                                  */
   if last.x then output;       /* the IF in this form only sends data to the output 
                                   set when the condition is true. The condition
                                   here is that the current X value is the last 
                                   of this group.
                                 */

   drop y;                      /* remove the current value of Y when the data is output*/
run;

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 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
  • 3 replies
  • 2648 views
  • 1 like
  • 3 in conversation