BookmarkSubscribeRSS Feed
david27
Quartz | Level 8

Hello,

 

I have the below code working:

I need help with looping the below code such that the loop works for length(name) number of times and new_name is completely replaced except for the first string.

I want all the _1 and _2... concatenated to produce a variable named new_name.

 

Please help.

 

%let string1=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;
%let string2=`~!@#$%^&*()-=_+[]\{}|:,./<>?†°¬ŒÏ¾ABCDEFGHIJKLMNOPQRSTUVWXYZ;
%put string1=&string1.;
%put string2=&string2.;

data encrypt;
set sashelp.class;
length new_name $25.;
new_name=name;
len=length(name);

new_name_1=substr(new_name,1,1);
new_name_2=substr(new_name,2,1);
index_1=index("&string1.",trim(new_name_1) );
index_2=index("&string1.",trim(new_name_2) );
_1=substr("&string2.",index_1,1);
_2=substr("&string2.",index_2,1);

drop new_name_1 new_name_2 index_1 index_2 len;
run;
3 REPLIES 3
ballardw
Super User

I think you may be looking for function TRANSLATE which will replace characters from one list with those of another.

You do realize that your two STRING variables are of different lengths though.

 

data encrypt;
set sashelp.class;
length new_name $25.;
new_name=name;

new_name_1=translate(name,"&string2.","&string1");

run;
david27
Quartz | Level 8

Thank You @ballardw

Yes this is simple. But then translate would work only for character variables. correct?

I want help with building something that would also encrypt numeric variables.

 

Thanks

 

ballardw
Super User

@david27 wrote:

Thank You @ballardw

Yes this is simple. But then translate would work only for character variables. correct?

I want help with building something that would also encrypt numeric variables.

 

Thanks

 


To tell you the truth I couldn't actually follow the logic you were using in a short period of time. And any approach at all close will not "encrypt numeric" values because once you do something like this they are character anyway. So using PUT with your numeric value to create character value and then translate.

 

charactervalue = translate (put(numericvar,f8.2),"&string2.","&string1");

The decrypt would be to do the reverse translate and use input.

 

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 692 views
  • 0 likes
  • 2 in conversation