03-03-2023
Luke3
Obsidian | Level 7
Member since
03-03-2020
- 12 Posts
- 5 Likes Given
- 1 Solutions
- 0 Likes Received
-
Latest posts by Luke3
Subject Views Posted 3312 05-18-2022 06:27 AM 3319 05-18-2022 06:23 AM 3390 05-17-2022 09:23 AM 3411 05-17-2022 08:05 AM 3446 05-17-2022 07:17 AM 3464 05-17-2022 06:48 AM 3075 03-16-2020 10:42 AM 3125 03-11-2020 10:31 AM 3217 03-06-2020 06:06 AM 3440 03-03-2020 04:39 AM -
Activity Feed for Luke3
- Posted Re: The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-18-2022 06:27 AM
- Posted Re: The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-18-2022 06:23 AM
- Posted Re: The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-17-2022 09:23 AM
- Posted Re: The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-17-2022 08:05 AM
- Posted Re: The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-17-2022 07:17 AM
- Posted The routine PRXCHANGE was called using a regular expression that contains no replacement text on SAS Programming. 05-17-2022 06:48 AM
- Liked Re: set statement in a do loop vs set statement not in do loop for ChrisBrooks. 01-26-2022 03:03 PM
- Liked Re: How to read a dataset while looping on another for Astounding. 03-17-2020 08:36 AM
- Liked Re: How to read a dataset while looping on another for RichardDeVen. 03-17-2020 08:35 AM
- Liked Re: How to read a dataset while looping on another for Patrick. 03-17-2020 08:35 AM
- Liked Re: How to read a dataset while looping on another for Patrick. 03-17-2020 08:35 AM
- Posted Re: How to read a dataset while looping on another on SAS Programming. 03-16-2020 10:42 AM
- Posted Re: How to read a dataset while looping on another on SAS Programming. 03-11-2020 10:31 AM
- Posted Re: How to read a dataset while looping on another on SAS Programming. 03-06-2020 06:06 AM
- Posted Re: How to read a dataset while looping on another on SAS Programming. 03-03-2020 04:39 AM
- Posted Re: How to read a dataset while looping on another on SAS Programming. 03-03-2020 04:21 AM
- Posted How to read a dataset while looping on another on SAS Programming. 03-03-2020 03:49 AM
-
Posts I Liked
Subject Likes Author Latest Post 1 2 1 1 2
05-18-2022
06:27 AM
@s_lassen wrote:
If, for instance, you wanted to replace the found expression with an "X", your PRX expression should look like this:
'~^[0-9\./]+~X~'
What is the string you are searching for, and what do you want to replace it with?
I want to extract ^[0-9\./]+ from the string, so like saying I want to replace the whole string with that.
... View more
05-18-2022
06:23 AM
@andreas_lds wrote:
@Luke3 wrote:
It doesnt work if the string doesn't start with a number. From the example it wasn't clear, but it's not guaranteed that the string starts with a number, also it can have special characters, so I'm not sure the anyalpha-1 solution would work. In the meanwhile I found this way:
Then, please, post data that contains all possible combinations of digits and letters that could exist and the expected result.
More eterogeneous data:
----- ----string helloworld 323.43astring 23hello(world*23.34.12) 1223/34anotherstring12.34 1234 12-43 13.34/34
The regular expression to extract is ^[0-9\./]+ (numbers, dots and slashes at the beginning). Expected result:
empty or skip
empty or skip
empty or skip
323.43
23
1223/34
1234
12
13.34/34
The solution I came up with is:
data table2(DROP = pattern start length);
set table1;
pattern = PRXPARSE('~^[0-9\./]+~');
call prxsubstr(pattern, name, start,length);
IF length>0 THEN name2 = SUBSTR(name, start , length);
run;
The solution proposed by @Ksharp should work if we add a check on the first character of the string:
data table2;
set table1;
IF ISNUMBER(SUBSTR(name,1,1) THEN name2 = prxchange('s/^([\d\.]+).*/\1/',1,name);
run;
Don't know if it's possible to do it with a single call to prxchange
... View more
05-17-2022
09:23 AM
It doesnt work if the string doesn't start with a number. From the example it wasn't clear, but it's not guaranteed that the string starts with a number, also it can have special characters, so I'm not sure the anyalpha-1 solution would work. In the meanwhile I found this way: data table2(DROP = pattern start length); set table1; pattern = PRXPARSE('~^[0-9\./]+~'); call prxsubstr(pattern, name, start,length); IF length>0 THEN name2 = SUBSTR(name, start , length); run;
... View more
05-17-2022
08:05 AM
I need numbers and dots starting from the beginning of the string, not all numbers and dots. So in line 4 it should be only 12, not 1212. Thanks.
... View more
05-17-2022
07:17 AM
I'm trying to extract the substring identified by the regular expression: numbers and dots starting from the beginning. Variable name2 should contain: 44 3.3. 22.22 12
... View more
05-17-2022
06:48 AM
Hi, function PRXCHANGE returns the error "The routine PRXCHANGE was called using a regular expression that contains no replacement text" and I can't find the reason. What I want to do is extract the substring identified by the regular expression. As a test, I get the same error on this example dataset: data table1; input name & $32.; datalines; 44fds 3.3.fdfsd 22.22fdfs 12dsd12 ; data table2; set table1; name2 = prxchange('~^[0-9\./]+~', -1, name); run; /*This gives the error*/ The regular expression is correct, in fact if you run this data table2; set table1; test = PRXMATCH('~^[0-9\./]+~',name); run; it runs correctly and the variable test is valorized with 1, so the regex was found. Thanks in advance, Luke
... View more
03-16-2020
10:42 AM
Hi Patrick, I tested your codes, all three, and they work. Great to learn about the hash objects, which I didn't know existed.
... View more
03-11-2020
10:31 AM
Hi RichardADeVenezia, I tested your code and it works, but with corrections. Datasets need to be sorted and the sequence in set 2 must start over for each group gender,ageclass. So the correct code is: proc sort data=one;
by gender ageclass;
run;
proc sort data=two;
by gender ageclass
run;
data one_v;
set one;
by gender ageclass;
if first.ageclass then pair=0; else pair+1;
run;
data two_v;
set two;
by gender ageclass;
if first.ageClass then seq=0; else seq+1;
pair = floor(seq/2);
rowseq + 1;
run;
proc sql;
create table want as
select two.subset, two.gender, two.code, two.ageclass, one.code as assigned_code
from two_v as two
left join one_v as one
on one.gender=two.gender
& one.ageclass=two.ageclass
& one.pair=two.pair
order by rowseq
; I will mark your post as the solution but readers mind to read this post too because without the corrections the algorithm doesn't work. I'll use this post to share a solution I elaborated myself and hopefully to get your comments on it: /* inner loop, on dataset two */
%macro loop_two(code_v,gender_v,ageclass_v);
data two (drop = counter);
set two;
length assigned_code $25;
if _n_ = 1 then
counter = 0;
if (counter<4 & assigned_code = ' ' & gender = "&gender_v" & ageclass = "&ageclass_v") then
do;
assigned_code = "&code_v";
counter+1;
end;
run;
%mend;
/* outer loop, on dataset one */
data _null_;
set one;
call execute('%loop_two('||code||','||gender||','||ageclass||')' );
run; It works but it's slow, because it calls a macro for each observation in dataset one. I wonder if there is a faster way to do it with a double loop... A way to build an array in memory with a single read from dataset one, maybe with macro arrays? But macro arrays can only get a single column of a dataset, they can't take the whole dataset as a matrix, can they?
... View more
03-06-2020
06:06 AM
Hi Astounding, I tested your code, but it does the wrong thing. It only keeps distinct combinations of gender, ageclass from dataset1, but no, we must keep all lines of dataset1 and assign their code to first two (or n) lines of dataset2 that match gender, ageclass and don't have an assigned code yet. I guess we can't use merge with duplicates, can we?
... View more
03-03-2020
04:39 AM
Hi, this solution would perform a join on matching fields, so it wouldn't work as the algorithm I better explained above, would it? Criteria variables are not unique and I need to match only a given number of poeple per code, not all (see algorithm above).
... View more
03-03-2020
04:21 AM
Dataset1 : subset gender code ageclass 1 M 001 20-29 1 M 002 20-29 1 F 003 30-39 Dataset2 : subset gender code ageclass 2 M 004 20-29 2 M 005 20-29 2 M 006 20-29 2 M 007 20-29 2 F 008 30-39 2 F 009 30-39 2 F 010 30-39 2 F 011 20-29 I expect as a result Dataset2 (or a new dataset) modified as follows: Dataset2 : subset gender code ageclass assigned_code 2 M 004 20-29 001 2 M 005 20-29 001 2 M 006 20-29 002 2 M 007 20-29 002 2 F 008 30-39 003 2 F 009 30-39 003 2 F 010 30-39 2 F 011 20-29 The algorithm must do this: - read the first line from dataset1 (lets call it L1) - look for the first two lines of dataset2 that match L1.gender, L1.ageclass, have assiged_code empty, and assign L1.code to assigned_code - read the second line from dataset1 (lets call it L2) - look for the first two lines of dataset2 that match L2.gender, L2.ageclass, have assiged_code empty, and assign L2.code to assigned_code and so on for all the lines of dataset1. The subset column just identifies the dataset.
... View more
03-03-2020
03:49 AM
Hi, I have two datasets: a dataset of codes and a dataset of people. I need to read the dataset of codes and, for each code, scan the dataset of people and assign it to some people according to some criteria. For example: - read code 1 - assing it to John, Mark and Carla - read code 2 - assign it to Peter, Rita and Katia - read code 3 - assign it to Dave, Sue and Carl ... and so on for all codes How to do it? EDIT: Please notice that the accepted solution must be modified as I wrote in my answer to it
... View more