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

data have;

input accid   branch;

datalines;

1001  pune,hyderabad,banglore

1002  haryana,delhi

1003  shimla

;

run;

 

 

data want;

input accid  branch;

datalines;

1001  pune

1001  hyderabad

1001  banglore

1002  haryana

1002  delhi

1003  shimla

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

In line with @andreas_lds:

data have;
input accid $4. branch $25.;
datalines;
1001 pune,hyderabad,banglore
1002 haryana,delhi
1003 shimla
;
run;

data want (drop = branch i);
	set have;
		do i = 1 to countw(branch, ","); /* i becomes a counter for the commas */
			_branch = scan(branch, i, ","); /* notice the i becomes the <count> argument in the SCAN function. The loop iterates over the number of commas.*/
			output; /* Output each separate branch before returning to top of loop. */
		end;
run;
accid _branch 
1001 pune 
1001 hyderabad 
1001 banglore 
1002 haryana 
1002 delhi 
1003 shimla 

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

Problems like your have asked and answered so many times, i wonder why you haven't found something to adaptable to your needs.

You will have to use:

  • a loop with countw to get the number of interations
  • scan to extract each word / city
  • output to write an obs for each city
maguiremq
SAS Super FREQ

In line with @andreas_lds:

data have;
input accid $4. branch $25.;
datalines;
1001 pune,hyderabad,banglore
1002 haryana,delhi
1003 shimla
;
run;

data want (drop = branch i);
	set have;
		do i = 1 to countw(branch, ","); /* i becomes a counter for the commas */
			_branch = scan(branch, i, ","); /* notice the i becomes the <count> argument in the SCAN function. The loop iterates over the number of commas.*/
			output; /* Output each separate branch before returning to top of loop. */
		end;
run;
accid _branch 
1001 pune 
1001 hyderabad 
1001 banglore 
1002 haryana 
1002 delhi 
1003 shimla 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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