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.
A | B | C | D |
101 | Aks | likwise asksa | 422 |
102 | Pat | Bck | 23 |
103 | Bha | asas | 122 |
104 | Dok | asoio asjka | 124 |
and wanted to output like split those values into seperate rows like below using SAS code
A | B | C | D |
101 | Aks | likwise | 422 |
101 | Aks | asksa | 422 |
102 | Pat | Bck | 23 |
103 | Bha | asas | 122 |
104 | Dok | asoio | 124 |
104 | Dok | asjka | 124 |
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!!
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;
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;
Thank you SASJedi. I have tried this and works fine for me. . Now I can modify as per my requirements.
Thank you so much
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.