Reputation: 13
I would like to parse a sas variable into several variables based on the the space (or special characters) between 2 words. I would like the code to place the word in a new column ONLY when there is > 2 spaces (or special chars) between the words. for example below I have used ^ instead of space here.
if TEXT="
LEVEL^OF^RENAL^IMPAIRMENT^^^^^^^SEVERE,^n^(%)^^^^^^^52^(^1.9)^^^^^^^^^ 133^(^1.5)"
then col1= "LEVEL^OF^RENAL^IMPAIRMENT"
col2= "SEVERE,^n^(%)"
col3="52^(^1.9)"
col4="133^(^1.5)"
it would be great if the code can count the number of columns for each record upto a max of say 15 columns.........Regards, Z
Upvotes: 1
Views: 1385
Reputation: 1807
You can use a regex to convert your data in delimted file that the scan()
function can understand. For example this statement,
text2=prxchange('s/(\^| ){2,}/~/',-1,TEXT);
results in text2 of
LEVEL^OF^RENAL^IMPAIRMENT~SEVERE,^n^(%)~52^(^1.9)~133^(^1.5)
which is a version of your data that uses ~
as a delimeter. Now you can use scan()
, like this:
col1 = scan(text2,1,'~');
col2 = scan(text2,2,'~');
col3 = scan(text2,3,'~');
col4 = scan(text2,4,'~');
You might have to modify the regex to capture all the spaces and special characters that can make up a delimiter. In my example, the (\^| )
means "a string that is either a single ^
or a single space". Also, if you're data contains ~
you'll probably want to choose some other characther.
Upvotes: 2