data cust; input cust_id $8.; datalines; E4349567 C4127658 C6267260 E5211402 K4729617 G6646262 ; run; PROC PRINT DATA=CUST; RUN; data test2; set cust; string=COLLATE(1,256); high4=substr(string,55); cust_id=translate(cust_id, high4, string); run; proc print data=test2; run;
Can anyone let me know why I only have 1 entry in my output datset test2, I should have same number as my input datatset cust, right?
When I run your code, I have 6 observations in both.
When something unexpected happens, always (as in ALWAYS) post your complete log.
data test2; set cust; string=COLLATE(1,256); high4=substr(string,55); cust_id=translate(cust_id, high4, string); run;
what would be the code in sas to reverse the above?
Just reverse the from & to in the translate function.
data test2;
set cust;
string=COLLATE(1,256);
high4=substr(string,55);
cust_id_out_1=translate(cust_id, high4, string);
cust_id_out_2=translate(cust_id_out_1,string,high4);
run;
data custinfo; input cust_id $8.; datalines; E4349567 C4127658 C6267260 E5211402 K4729617 G6646262 ; run; data test1; set custinfo; string=COLLATE(1,256); high4=substr(string,55); cust_id_out_1=translate(cust_id, high4, string); cust_id_out_2=translate(cust_id_out_1,string,high4); run; proc print data=custinfo; run; proc print data=test1; run;
I tried but cannot see cust_id_out_1 or cust_id_out_2, but no error though
Try starting a fresh SAS session and run it again. The code works for me.
@HeatherNewton Works for me without any issues once I apply format $hex<n>. to print variables with values that can contain control characters and the like.
data custinfo;
input cust_id $8.;
datalines;
E4349567
C4127658
C6267260
E5211402
K4729617
G6646262
;
run;
data test1;
set custinfo;
string=COLLATE(1,256);
high4=substr(string,55);
cust_id_out_1=translate(cust_id, high4, string);
cust_id_out_2=translate(cust_id_out_1,string,high4);
format string $hex512. high4 $hex110. cust_id_out_1 $hex16.;
run;
proc print data=custinfo;
run;
proc print data=test1;
run;
Btw: If you really want to create and store a list of the first 256 characters in the collating sequence then you need to explicitly define the length for variable string as else it will default to a length of only $200 in your code.
You also need to start counting at 0 and not 1.
And... if you want to ensure consistent results for running in any SAS environment then you need also to care about the Collating Sequence and define it explicitly.
I'd also consider to create a list of characters for variable string that only contains printable characters and that certainly don't contain control characters.
data test2; set cust; string=COLLATE(1,256); high4=substr(string,55); cust_id=translate(cust_id, high4, string); run;
does these create binary code? they look like symbols e.g.
^@000^0^0
By shifting characters "up" by 55, you will eventually get results in the range of 128-182. Within this range, you have bytes which initiate a multi-byte UTF sequence, or are only allowed as UTF continuation bytes.
If you need a reversible process which creates readable results and makes it at least a little hard to reverse for a third person, create a lookup table with values created by a random function. How you create such depends on the structure of the values to be encoded.
can I use encoding=utf8 to display it?
I used encoding=utf8 to convert the file to csv and use codepage=1208 (which is also for utf8)
to load into db2
however the content splited up into two rows with a cache return between
so e.g. ^@000^@^@
before ^@000 ^@^@
does it work if I use double quote to enclose it before converting into csv
but I tired it keep giving me """"""""""""
If you want something readable which will reliably load into another software, you need a completely different approach. This crude "shift by 55" is clearly not the way to go.
@HeatherNewton The very similar forum discussion here provides multiple options how you could approach this.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.