BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hello:

 

I would like to assign a number based on the codes below instead of typing 1 to unlimited.  Please help.

 

Thanks.

 

data test;

set test;

if prxmatch("m/date/i",var) > 0 then found=1;

else if prxmatch("m/den/i",var) > 0 then found=2;

else if prxmatch("m/pcr/i",var) > 0 then found=3;

.

.

.

else found=0;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

I'm assuming the appearance of the strings in question are mutually exclusive, but specifying the strings in the desired "found" order and the iterating through might do what you want:

 

data have;
    input @1 text $50.;
    array strs{3} $ ('date' 'den' 'pcr');
    do i = 1 to dim(strs);
        if prxmatch(prxparse(cats('/', strs(i), '/')), text) > 0 then found = i;
    end;
datalines;
the word is date 
you said pcr
den is the word
;

View solution in original post

12 REPLIES 12
collinelliot
Barite | Level 11

I'm assuming the appearance of the strings in question are mutually exclusive, but specifying the strings in the desired "found" order and the iterating through might do what you want:

 

data have;
    input @1 text $50.;
    array strs{3} $ ('date' 'den' 'pcr');
    do i = 1 to dim(strs);
        if prxmatch(prxparse(cats('/', strs(i), '/')), text) > 0 then found = i;
    end;
datalines;
the word is date 
you said pcr
den is the word
;
ybz12003
Rhodochrosite | Level 12

I have 300 rows.  Is there any way to avoid input all of the names?  Thanks.

collinelliot
Barite | Level 11

It would help me if you'd define your issue better. 300 rows is not an issue. It's the number of strings you want to match and assign an order to that would make this tedious.

ybz12003
Rhodochrosite | Level 12

I have to repeat use prxmatch statement over 100 times,  I don't know when will be the end of the string.  My goal is to automatic assign number to the end of the string, insteading of typing 'found=number' many times.

Reeza
Super User

Then you need to change the logic. Given how you phrased the question the answer is correct. But that isn't your actual question.

 

So post a better example of your question. Also, make sure you try the answer. 

 

 

collinelliot
Barite | Level 11

 

What I'm proposing would require modifying one line in your code to accomodate the strings you're looking for:

 

array strs{3} $ ('date' 'den' 'pcr');

 

If you really have 150+ strings that you want to assign to "found = ##," do these strings reside in data in any way? If that's really the case, I think you'll be looking for a macro solution, but since I have .005% confidence that I understand you're problem, I'll wait until it's more clear, at which point you'll probably have many other valid solutions.

Reeza
Super User

Why does the number of rows matter? 

ballardw
Super User

how many codes? How many "found" values need assignment?

 

Or are just looking to add a row number to the data?

ybz12003
Rhodochrosite | Level 12

I estimate it's about 300.

ybz12003
Rhodochrosite | Level 12

My goal is to track which name need to be change in the future when I found the unmatch string.

kiranv_
Rhodochrosite | Level 12

I would rather maintain a reference table for this kind of thing with record_load_dt as your audit columns.your table will have values as den 1 06/07/2017 some thing like thing that and you update this table as per your requireement or whenever you get new value

ballardw
Super User

@ybz12003 wrote:

My goal is to track which name need to be change in the future when I found the unmatch string.


Is this supposed to be the name of a variable? If so your process is likely flawed, variable names should not change if they mean the same thing. If the value represented should be associated with another item such as a location or activity then the a separate varaible to hold the value of that location, activity or whatever is better instead of having names like Value_Cityx Value_CityY Value_Cityz.

 

You should be controlling the names of variables when they are created as well as other chracteristics as variable type, format, length and label.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1922 views
  • 1 like
  • 5 in conversation