BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jonatan_velarde
Lapis Lazuli | Level 10

Good day SAS friends:

 

i have a data set containing information from individuals from school, and i know what they eat at lunch, each information is separated by semicolon ";", as follows

 

data have;

input student answers$;

cards;

1 bread; soda

2 bread; soda; water

3 water; bread

4 soda; crackers

5 crackers

6 crakers; water

;

 

student bread soda water crackers
1 yes yes no no
2 yes yes yes no
3 yes no yes no
4 no yes no yes
5 no no no yes
6 no no yes yes

 

Thanks in advance

 

 

the new data set that i want to create based in this is:

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First, create a long dataset:

data long;
set have;
answer = "yes";
do i = 1 zo countw(answers,';');
  meal = scan(answers,i,';');
  output;
end;
keep student meal answer;
run;

Then transpose to wide:

proc transpose data=long out=wide;
by student;
var answer;
id meal;
run;

If necessary, you can replace any missing values in a follow-up step with "no". The beauty of the transpose is that you do not need to know all the meals beforehand.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

First, create a long dataset:

data long;
set have;
answer = "yes";
do i = 1 zo countw(answers,';');
  meal = scan(answers,i,';');
  output;
end;
keep student meal answer;
run;

Then transpose to wide:

proc transpose data=long out=wide;
by student;
var answer;
id meal;
run;

If necessary, you can replace any missing values in a follow-up step with "no". The beauty of the transpose is that you do not need to know all the meals beforehand.

novinosrin
Tourmaline | Level 20

Hi @jonatan_velarde  By all means @Kurt_Bremser  's solution is indeed clean as it is data driven that gives you the comfort of not having to be concerned about what's in your data. However, should you know beforehand that there exists  only 4 distinct lunch items in your data, i.e.   

bread soda water crackers

you may consider going lazy declaring an array like-

data have;
input student answers & $20.;
cards4;
1 bread; soda
2 bread; soda; water
3 water; bread
4 soda; crackers
5 crackers
6 crakers; water
;;;;

data want;
 set have;
 array food(*)$10 bread	soda water crackers;
 array t(4) $10 _temporary_ ('bread','soda','water','crackers');
 do _n_=1 to dim(food);
  food(_n_)=ifc(find(answers,strip(t(_n_))),'YES','NO');
 end;
 drop answers;
run;
  

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 844 views
  • 0 likes
  • 3 in conversation