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

Hello,

 

I would ask for help on the below please.

 

I have a dataset in the following format:

 

 

data have;
  INFILE DATALINES DELIMITER=',' DSD;
input Branch $ Employee Products $50.;
datalines;
A, 1, Ball, Paper, Pen, Glass, Car,
B, 1, Dog, Cat, Bird, Water, Pencil,
B, 2, Ball, Paper, Pen, Glass, Car,
A, 2, Dog, Cat, Bird, Water, Pencil,
run;

The column "Products" contains values separated by commas. 

I want to create a new row with each of these values.

 

 

The new structure should be as follows:

 

data want;
input Branch $ Employee Products $50.;
datalines;
A 1 Ball
A 1 Paper
A 1 Pen
A 1 Glass
A 1 Car
B 1 Dog
B 1 Cat
B 1 Bird
B 1 Water
B 1 Pencil
A 2 Ball
A 2 Paper
A 2 Pen
A 2 Glass
A 2 Car
B 2 Dog
B 2 Cat
B 2 Bird
B 2 Water
B 2 Pencil
;
run;

Any help please.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

This is a pretty simple DO loop where you use the SCAN function to find the i-th "word", where "word" is delimited by a comma.

 

data want;
    set have;
    do i=1 to countw(products,',');
        product=scan(products,i,',');
        if not missing(product) then output;
    end;
    drop i products;
run;

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

This is a pretty simple DO loop where you use the SCAN function to find the i-th "word", where "word" is delimited by a comma.

 

data want;
    set have;
    do i=1 to countw(products,',');
        product=scan(products,i,',');
        if not missing(product) then output;
    end;
    drop i products;
run;

 

--
Paige Miller

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
  • 1 reply
  • 202 views
  • 0 likes
  • 2 in conversation