BookmarkSubscribeRSS Feed
piddy_
Fluorite | Level 6

Hi 

 

I have to change different symbols and specific letters for the same variable.

So far I have just been doing like below and creating one new variable each time I change something but I am sure there must be a more efficient way:

 

I hope somebody can help.

 

Thanks in advance.

Filename_WF12=left(prxchange('s/[¤]/%c2%a4/oi',-1,trim(Filename_WF11)));  
Filename_WF13=left(prxchange('s/[@]/%40/oi',-1,trim(Filename_WF12)));  
Filename_WF14=left(prxchange('s/['']/%27/oi',-1,trim(Filename_WF13)));  
Filename_WF15=left(prxchange('s/[;]/%3b/oi',-1,trim(Filename_WF14))); 
Filename_WF16=left(prxchange('s/[`]/%60/oi',-1,trim(Filename_WF15))); 
Filename_WF17=left(prxchange('s/[´]/%c2%b4/oi',-1,trim(Filename_WF16))); 
Filename_WF18=left(prxchange('s/[¨]/%c2%a8/oi',-1,trim(Filename_WF17))); 
Filename_WF19=left(prxchange('s/[§]/%c2%a7/oi',-1,trim(Filename_WF18))); 

/*------ and so on  ------*/

 

 

 

4 REPLIES 4
Sajid01
Meteorite | Level 14

I understand your anguish in using the regular expressions.
It would help if you can give us an idea of how the original names/tokens are.
Be aware that there cannot be a single general  formula or function to address all the issues.

piddy_
Fluorite | Level 6

Hi again

 

The dataset is about 2.5 million records. Combining some of the variables creates a link that can be used in a web browser to download different information from our system. I will hereafter use a VBA script in Excel to download the files. After controlling the filename in raw data and the filename in the web browser, its not always the same, some characters changes and that's the reason for my code. Every single record for the filename variable is different.

 

Values in the filename variable could be some thing like this:

 

EmailImport-VS {{POST}}Advisering pr. 30.12.2013 [Ref.nr.].pdf
{{SERVICE}} Postkasse [Autotitel=2018A12A1327] [Ref.nr.=49e392c5d6-df75-4687-ba81-effd3a4a752a.pdf

In the webbrowser values for }, {, [, ], = and etc. has its own values and that's the reason for my coding. 

 

I thought maybe there was a better way than creating 40 variables where I each time changes something different. In the end I only need the last variable where all preceding things have been changed. 

Maybe nested regular expressions? or something else ?

 

Data test_2;
set test_1;

/*characters that's the same in raw data and in the web browser - identifying records that's different*/

special_char=compress(Filnavn_WF,'abcdefghijklmnopqrstuvwxyz0123456789.()!-','U');

if not missing(special_char) then do;
 
 Filename_WF1=left(prxchange('s/[¤]/%c2%a4/oi',-1,trim(Filename_WF)));  
 Filename_WF2=left(prxchange('s/[@]/%40/oi',-1,trim(Filename_WF1)));  
 Filename_WF3=left(prxchange('s/['']/%27/oi',-1,trim(Filename_WF2)));  
 Filename_WF4=left(prxchange('s/[;]/%3b/oi',-1,trim(Filename_WF3))); 
 Filename_WF5=left(prxchange('s/[`]/%60/oi',-1,trim(Filename_WF4))); 
 Filename_WF6=left(prxchange('s/[´]/%c2%b4/oi',-1,trim(Filename_WF5))); 
 Filename_WF7=left(prxchange('s/[¨]/%c2%a8/oi',-1,trim(Filename_WF6))); 
 Filename_WF8=left(prxchange('s/[§]/%c2%a7/oi',-1,trim(Filename_WF7))); 

/*.... etc. */
                                                                                                                                                
end;

run; 

 

Sajid01
Meteorite | Level 14

Hello @piddy_ 

Thanks for the reply.
I understand the pain.

As the saying goes "the wearer knows where the shoe pains" you know the issue better.

With this statement   "In the end I only need the last variable where all preceding things have been changed" in mind,   my approach would be to reverse the string and take the required portion.

An example of my approach is the following snippet. I have an feeling that creating 40 variables would not be needed.

I have used a similar approach but in Korn shell scripting quite some time back.

I know this will not end your miseries but may show some direction.

For ease of understanding I have created multiple variables str1,str2,str3 and str4..

Similarly I have used multiple puts to trace what is happening.

Best of luck in your endeavor.
I would appreciate if you can share your success. That will enlighten many of us.


data _null_;

/* This is the string from which you are going to extract the word*/
str1="EmailImport-VS {{POST}}Advisering pr. 30.12.2013 [Ref.nr.].pdf {{SERVICE}} Postkasse [Autotitel=2018A12A1327] [Ref.nr.=49e392c5d6-df75-4687-ba81-effd3a4a752a.pdf";

/* reverse it*/
str2=reverse(str1);

put=;
/* Taking "=" as the marker*/
str3=substr(str2,1,find(str2,'=')-1);
put str3=;

/* Reverse again to get back*/
str4=reverse(str3);
put str4=;
run;

ballardw
Super User

I can understand leaving the original variable alone but wouldn't creating a single variable work just successively reprocessing it?

 

Such as

 Filename_WF1=left(prxchange('s/[¤]/%c2%a4/oi',-1,trim(Filename_WF)));  
 Filename_WF1=left(prxchange('s/[@]/%40/oi',-1,trim(Filename_WF1)));  
 Filename_WF1=left(prxchange('s/['']/%27/oi',-1,trim(Filename_WF1)));  
 Filename_WF1=left(prxchange('s/[;]/%3b/oi',-1,trim(Filename_WF1))); 
 Filename_WF1=left(prxchange('s/[`]/%60/oi',-1,trim(Filename_WF1))); 
 Filename_WF1=left(prxchange('s/[´]/%c2%b4/oi',-1,trim(Filename_WF1))); 
 Filename_WF1=left(prxchange('s/[¨]/%c2%a8/oi',-1,trim(Filename_WF1))); 
 Filename_WF1=left(prxchange('s/[§]/%c2%a7/oi',-1,trim(Filename_WF1))); 

then you only have one "new" variable.

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 559 views
  • 3 likes
  • 3 in conversation