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 

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