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

Hello Experts,

 

I have below table in SAS with two values merged in row for column C like below and this table is imported from Excel file so Alt+Enter used to enter two values in same row.

ABCD
101Akslikwise

asksa
422
102PatBck23
103Bhaasas122
104Dokasoio

asjka
124

 and wanted to output like split those values into seperate rows like below using SAS code

 

ABCD
101Akslikwise422
101Aksasksa422
102PatBck23
103Bhaasas122
104Dokasoio124
104Dokasjka124

Is it possible to do ? I tried a lot ways to solve this issue but could not able to solve this. Please  can You please  suggest a way to resolve this issue. it would be much appriciated.  Thanks in advance!!

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

Try this:

/* Assign a libref to the Excel file */
libname xl xlsx "have.xlsx";
data want;
	/* Read the data from the Excel tab */
	/* Renaming the C column so we can create a new C */
	set xl.have (rename=(c=_C));
	/* Set up the new C variable */
	length C $10;
	/* 0D0A hex is the CTRL-Enter key value */
	_ctrlEnter='0D0A'x;
	/* If the text contains CTRL-Enter, split it up */
	if find(_C,'0D0A'x) then do;
		/* Count the number of CTRL-Enter character in the text */
		/* and output a new row for each value */
		do _i=1 to countc(_C,_ctrlEnter);
			C=scan(_C,_i,_ctrlEnter);
			output;
		end;
	end;
	/* Otherwise, just write out a record */
	else do;
		C=_c;
		output;
	end;
	/* Get rid of all the temporary variables (names begin with _)*/
	drop _:;
run;
/* Clear the Exel libref */
libname xl clear;
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

2 REPLIES 2
SASJedi
SAS Super FREQ

Try this:

/* Assign a libref to the Excel file */
libname xl xlsx "have.xlsx";
data want;
	/* Read the data from the Excel tab */
	/* Renaming the C column so we can create a new C */
	set xl.have (rename=(c=_C));
	/* Set up the new C variable */
	length C $10;
	/* 0D0A hex is the CTRL-Enter key value */
	_ctrlEnter='0D0A'x;
	/* If the text contains CTRL-Enter, split it up */
	if find(_C,'0D0A'x) then do;
		/* Count the number of CTRL-Enter character in the text */
		/* and output a new row for each value */
		do _i=1 to countc(_C,_ctrlEnter);
			C=scan(_C,_i,_ctrlEnter);
			output;
		end;
	end;
	/* Otherwise, just write out a record */
	else do;
		C=_c;
		output;
	end;
	/* Get rid of all the temporary variables (names begin with _)*/
	drop _:;
run;
/* Clear the Exel libref */
libname xl clear;
Check out my Jedi SAS Tricks for SAS Users
india2016
Pyrite | Level 9

Thank you SASJedi. I have tried this and works fine for me. . Now I can modify as per my requirements.

 

 

Thank you so much

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 101 views
  • 2 likes
  • 2 in conversation