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

Hi everyone. I was given a data in below structure:

Name    Age Hobby                                  Color
Ben       12    Movie, Study                        Blue, Red
Fenny     27   Jogging, Eating                   White, Red

 

and was asked to convert into this format:

Name  Age  Hobby  Color
Ben     12     Movie   Blue
Ben     12     Movie   Red
Ben     12     Study    Blue
Ben     12     Study    Red
Fenny  27    Jogging White
Fenny  27    Jogging Red
Fenny  27    Eating    White
Fenny  27    Eating    Red

 

I have been doing research for the past 3 hours and i do not find any straight forward answer to that.

I was thinking to use below logic, which i havent tried yet as my work PC is not at home today.

First step, find way to split the delimiter comma. Then, if first.name then populate the value in delimiter into column color and hobby.

However, I really cannot think of the proper script for that. Can anyone give some hint on the script needed? even if there is no full code is fine, as i just need some logic explanation.

data want;
set have;
if first.name then do;
     

end;

run;

 

 

 

 

 

Scenario 2

I have another new file and i was asked to generate another output.

Name    feeder_in    feeder_out   NickName
ABBA    1,2              A,B              ABBA
POLA    1,2              C,D,E          CONS POLA

and the desire output:

Name             feeder_final    
ABBA              1                
ABBA              2                 
ABBA              A
ABBA              B
POLA              1                 
POLA              2
CONS POLA         C
CONS POLA         D
CONS POLA         E

 

 

This is different from the earlier scenario.

 

Does anyone have clue for scenario 2?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

For each record, count the number of hobbies, count the number of colors, and then for each color/hobby combination, output a new record.

 

/* UNTESTED CODE */

data want;
    set have;
    length newhobby newcolor $ 10;
    do i=1 to countw(hobby,',');
         do j=1 to countw(color,',');
              newhobby=scan(hobby,i,',');
              newcolor=scan(color,j,',');
              output;
         end;
    end;
    drop i j hobby color;
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

For each record, count the number of hobbies, count the number of colors, and then for each color/hobby combination, output a new record.

 

/* UNTESTED CODE */

data want;
    set have;
    length newhobby newcolor $ 10;
    do i=1 to countw(hobby,',');
         do j=1 to countw(color,',');
              newhobby=scan(hobby,i,',');
              newcolor=scan(color,j,',');
              output;
         end;
    end;
    drop i j hobby color;
run;
--
Paige Miller
imdickson
Quartz | Level 8

Hi @PaigeMiller  and @Kurt_Bremser . Thanks for the code. It is working perfectly. However, would it be able to tweak to cater for Scenario 2 that i just put in in my first post?

PaigeMiller
Diamond | Level 26

@imdickson wrote:

Hi @PaigeMiller  and @Kurt_Bremser . Thanks for the code. It is working perfectly. However, would it be able to tweak to cater for Scenario 2 that i just put in in my first post?


It is so much easier to follow when new information is added at the bottom of the thread, rather than at the top. Could you please make Scenario 2 the next message in this thread?

--
Paige Miller
Kurt_Bremser
Super User
data want;
set have (rename=(hobby=hobby_old color=color_old));
do hc = 1 to countw(hobby_old,',');
  hobby = scan(hobby_old,hc,',');
  do cc = 1 to countw(color_old,',');
    color = scan(color_old,cc,',');
    output;
  end;
end;
drop color_old hobby_old hc cc:
run;

Just two nested loops and use of countw() and scan().

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 2637 views
  • 3 likes
  • 3 in conversation