BookmarkSubscribeRSS Feed
Ddolic
Calcite | Level 5

Dear all.

I have a String variable in a Dataset with manually entered text. I try to split the text into chunks, while the delimiter consists of more than one character and the can be one or more expressions of this kind. Example:

"0 - Nothing entered or found. 1 - One or more expression- or else - entered. 99 - Missing"

Should become

Field1 Field2

0     Nothing entered or found.

1     One or more expression- or else - entered

99   Missing

I am looking for the 0 - Part using regular expressions (\d+ ?)-

Now I'm search for a possibility to not only find the first occurence of the expressions. Any help is appreciated.

Best Regards

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

You could use scan:

data have;

  a="0 - Nothing entered or found. 1 - One or more expression- or else - entered. 99 - Missing";

run;

data want;

  set have;

  i=1;

  do while (scan(a,i,".") ne "");

    word=scan(a,i,".");

    output;

    i=i+1;

  end;

run;

Ddolic
Calcite | Level 5

Hi RW9.

Sorry, but no. The dot at the end of each sentence is not guaranteed. That's why I think about PRX. I have to look for the expression ([0-9] - ) as delimiter.

Thanks

Ddolic
Calcite | Level 5

I found one way:

Using PRXCHANGE('s/(d+ ?)-/$1=/', -1, mytextstring); I change the expression (0 - ) into (0 =). Now I can use SCAN Function.

FriedEgg
SAS Employee

Assuming you do not have numbers embedded in the strings (like in your example) you can use the following:

(\d+) - ([^\d]+)

Match 1:0 - Nothing entered or found.     0    30
Group 1:0     0     1
Group 2:Nothing entered or found.     4    26
Match 2:1 - One or more expression- or else - entered.    30    47
Group 1:1    30     1
Group 2:One or more expression- or else - entered.    34    43
Match 3:99 - Missing    77    12
Group 1:99    77     2
Group 2:Missing    82     7
Ksharp
Super User

You also could use SCAN(). But I would like to use Perl Regular Expression. Check  CALL PRXNEXT().

Code: Program

data have;
  a="0 - Nothing entered or found. 1 - One or more expression- or else - entered. 99 - Missing";
run;
data want;
set have;
length var $ 100;
do i=1 to countw(a,,'d');
  var=catx(' ',scan(a,i,,'kd'),scan(a,i,,'d'));output;
end;
run;

Xia Keshan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 917 views
  • 0 likes
  • 4 in conversation