BookmarkSubscribeRSS Feed
garag
Calcite | Level 5

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

4 REPLIES 4
kuridisanjeev
Quartz | Level 8

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

data_null__
Jade | Level 19

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.

kuridisanjeev
Quartz | Level 8

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

ArtC
Rhodochrosite | Level 12

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1368 views
  • 0 likes
  • 4 in conversation