DATA Step, Macro, Functions and more

translate function

Reply
Occasional Contributor
Posts: 11

translate function

[ Edited ]

I have text from file and I need to change some char.

but when I execute the code with a variable, the letters turn into something else. But if you insert the text manually, everything works correctly

 

%do i=1 %to &pfrcnt; 
%let tmp2=&decodedata; 
data tmp2; 
tmp2=%sysfunc(translate(&tmp2,'&','"')); 
run; 
%end;

Log

 tmp2 before translate 
<yearPeriodbeg="01.01.2016" end="31.12.2016" /> 
tmp2 after translate and error:
 tmp2= РРРР W WРРРР S SРРРР H РРРРРР СЃЃРЏ ЏµРЅЏµРР°Џ°РРЏ‡Џ°РРЏ°Џ·ЏµЏСЏ°РІРЏ°РЏrЏµРЅЏµР&ЏРЏ°РРРЏ°РРРЅЏ° 
ERROR: The value 'Р'nis not a valid SAS name. 


when I insert the text manually

data tmp;
	tmp=%sysfunc(translate('<yearPeriod beg="01.01.2016" end="31.12.2016"  />','&','"'));
run;

all is well

<yearPeriod beg=&01.01.2016& end=&31.12.2016&  />


 

 

Valued Guide
Posts: 564

Re: translate function

Sorry, but the code is unreadable.

You don't need %sysfunc when using translate in a data-step and you don't quote strings in macro-statements.

Super User
Posts: 10,217

Re: translate function

Text in a file is data, and should be treated as such. Therefore you DO NOT keep it in macro variables, which are meant to hold code parts. The macro preprocessor is a code generating language, not more or less.

So

data have;
input tmp2 $80.;
datalines4;
<yearPeriodbeg="01.01.2016" end="31.12.2016"
;;;;
run;

data want;
set have;
tmp2 = translate(tmp2,'&','"');
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 11

Re: translate function

Posted in reply to KurtBremser

It's work? but I have to get text from file and change all  ". But when insert text, I have error

  

Super User
Posts: 10,217

Re: translate function


@Liliya95 wrote:

It's work? but I have to get text from file and change all  ". But when insert text, I have error

  


Of course it does work. Copy my code and run it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 11

Re: translate function

Posted in reply to KurtBremser

sorry put a question mark by mistake.
But I can not write the text using the dataline.
There is a file, it is disassembled and a part is written in a variable

 all code:

......
%do i=1 %to &count; 
    
        %let data =%sysfunc(inputc("&&pfrbase64code&i", $base64x32767));
		%let FILE = &path/file_&i..xml;
        FILENAME FILE "&FILE" encoding='wcyrillic' LRECL=32767;

	data tmp2;

		tmp2=translate(&tmp2,'&','"');
	run;

     data _null_;
          
          fileFILE LRECL=32767;
          length tm $32767;
          put "&tmp2";

        run;


 %end;
Super User
Posts: 10,217

Re: translate function

So you want to replace a character in a file?

Start with

filename in "path_to_file" lrecl=32767;
filename out "path_to outfile" lrecl=32767;

data _null_;
infile in truncover;
file out;
input tmp $char32767.;
tmp = translate(tmp,'&','"');
put tmp;
run;

The only other (implicit) change that such a program does: it removes trailing blanks in lines, which should not be a problem with markup text.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,081

Re: translate function

You need to explain what you are actually trying to do.

Your posted code does not make much sense.

Just read that code line by line and ask yourself what each line is doing.

First you have wrapped the whole thing into a macro %DO loop. So I assume there is also a macro definition that this is part of.

%macro xxx ;
%do i=1 %to &count; 
 ...
%end;
%mend xxx;
%xxx;

Let's ignore that as irrelevant to your question.

So first you run this:

%let data =%sysfunc(inputc("&&pfrbase64code&i", $base64x32767));
%let FILE = &path/file_&i..xml;
FILENAME FILE "&FILE" encoding='wcyrillic' LRECL=32767;

Not sure what the purpose of the first line is as it does not appear to be used any where.  The other two lines look like an attempt to define a fileref name FILE.

 

Then you create this dataset named TMP2 by using a macro variable named TMP2 that has not been referenced before.

Not sure what purpose this step serves as the generated dataset is never used for anything.

data tmp2;
  tmp2=translate(&tmp2,'&','"');
run;

You then run another separate data step where it looks like your intent is to write to the fileref defined before.

data _null_;
  file FILE LRECL=32767;
  length tm $32767;
  put "&tmp2";
run;

But all you are trying to write is the unmodifed content of the previous read macro variable TMP2.

So again what was the purpose of the first data step with its TRANSLATE() function call?

You also define but never use a variable named TM.  Why is that line there?

 

Super User
Super User
Posts: 8,081

Re: translate function

Your first macro loop around a data step is just creating the same dataset with one observation and one variable over and over again.

 

What is the source of the actual data?

Ask a Question
Discussion stats
  • 8 replies
  • 95 views
  • 1 like
  • 4 in conversation