BookmarkSubscribeRSS Feed
Liliya95
Fluorite | Level 6

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&  />


 

 

8 REPLIES 8
andreas_lds
Jade | Level 19

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.

Kurt_Bremser
Super User

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;
Liliya95
Fluorite | Level 6

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

  

Kurt_Bremser
Super User

@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.

Liliya95
Fluorite | Level 6

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;
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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?

 

Tom
Super User Tom
Super User

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?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 8 replies
  • 939 views
  • 1 like
  • 4 in conversation