DATA Step, Macro, Functions and more

Reading all values of some columns and put them in a macrovariable

Reply
Contributor
Posts: 58

Reading all values of some columns and put them in a macrovariable

Hi,

I need to read all values of some columns of a dataset, managed them adding some fixed string and finally put them the result string in a variable.

Here you are the code:

%let bodymsgstring = '';
data outds;
    set WORK.inputds end=EOF;
       length tmpbodymsgstring $10000;
       retain tmpbodymsgstring '';
       tmpbodymsgstring = tmpbodymsgstring || col1 || trim(left(col2)) || "\n";

       if EOF then do;
          call symput('bodymsgstring', tmpbodymsgstring);
       end;
run;
%put bodymsgstring= &bodymsgstring;

The result string at the end of the elaboration is empty. Could someone explain me why?

Many thanks

Super Contributor
Posts: 276

Re: Reading all values of some columns and put them in a macrovariable

Hi Garag..

Lets try with this code....(Not tested)...

%let bodymsgstring = '';

data outds;

    length tmpbodymsgstring $10000;

    set WORK.inputds end=EOF;

      retain tmpbodymsgstring '';

       tmpbodymsgstring_1 = tmpbodymsgstring || col1 || trim(left(col2)) || "\n";

       if EOF then do;

          call symput('bodymsgstring', tmpbodymsgstring_1);

       end;

    drop tmpbodymsgstring;

    rename tmpbodymsgstring_1=tmpbodymsgstring;

run;

%put bodymsgstring= &bodymsgstring;

Regards..

Sanjeev.K

Respected Advisor
Posts: 3,777

Re: Reading all values of some columns and put them in a macrovariable

You need to TRIM TMPBODYMSGSTRING

tmpbodymsgstring = Trim(tmpbodymsgstring) || col1 || trim(left(col2)) || "\n";

You may also need to trim(col1) to get the desired result.

Super Contributor
Posts: 276

Re: Reading all values of some columns and put them in a macrovariable

Hi data_null_..

It is working fine  with trim function..

Can you please explain why is is not working without trim function???

Regards..

Sanjeev.K

Valued Guide
Posts: 632

Re: Reading all values of some columns and put them in a macrovariable

Without the outer TRIM function, the second set of characters are appended to the end of the 10,000 characters of the first (most of which are blank).  The TRIM removes the trailing blanks and allows you to append the values of the next record to immediately follow the '/n' of the preceding record.

Ask a Question
Discussion stats
  • 4 replies
  • 222 views
  • 0 likes
  • 4 in conversation