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.

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
  • 4 replies
  • 774 views
  • 0 likes
  • 4 in conversation