BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chithra
Quartz | Level 8

Dear all,

 

I need to concatenate the 3 variables in to one with out removing leading and trailing blanks.

 

This is in the datastep.

concatenate = trim(concatenate)||trim(value); --> this will remove the trailing blanks. I don't need to remove any blanks. I need what is exactly in the rows(including trailing and leading blanks).

 

could any one please help.

 

INPUT:

 

measures.PNG

MEASURES3=cnt_pol_COMBINE sum_pol_COMBINE cnt_pol_QA sum_pol_QA cnt_pol_FO sum_pol_FO cnt_pol_FACNP sum_pol_FACNP cnt_pol_PQS sum_pol_PQS cnt_pol_SURPLUS1 sum_pol_SURPLUS1 cnt_pol_SURPLUS2 sum_pol_SURPLUS2 cnt_

MEASURES3=pol_FACPROP sum_pol_FACPROP cnt_pol_OTHER sum_pol_OTHER cnt_ins_COMBINE sum_ins_COMBINE cnt_ins_QA sum_ins_QA cnt_ins_FO sum_ins_FO cnt_ins_FACNP sum_ins_FACNP cnt_ins_PQS sum_ins_PQS cnt_ins_SURPLUS1

MEASURES3= sum_ins_SURPLUS1 cnt_ins_SURPLUS2 sum_ins_SURPLUS2 cnt_ins_FACPROP sum_ins_FACPROP cnt_ins_OTHER sum_ins_OTHER

 

OUTPUT:

 

measures_ou.PNG

 

MEASURES3 = cnt_pol_COMBINE sum_pol_COMBINE cnt_pol_QA sum_pol_QA cnt_pol_FO sum_pol_FO cnt_pol_FACNP sum_pol_FACNP cnt_pol_PQS sum_pol_PQS cnt_pol_SURPLUS1 sum_pol_SURPLUS1 cnt_pol_SURPLUS2 sum_pol_SURPLUS2 cnt_pol_FACPROP sum_pol_FACPROP cnt_pol_OTHER sum_pol_OTHER cnt_ins_COMBINE sum_ins_COMBINE cnt_ins_QA sum_ins_QA cnt_ins_FO sum_ins_FO cnt_ins_FACNP sum_ins_FACNP cnt_ins_PQS sum_ins_PQS cnt_ins_SURPLUS1 sum_ins_SURPLUS1 cnt_ins_SURPLUS2 sum_ins_SURPLUS2 cnt_ins_FACPROP sum_ins_FACPROP cnt_ins_OTHER sum_ins_OTHER;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS stores character variables as fixed length.  So short strings are padded with spaces.

The LENGTH() function will return the location of the last non space.

If you want to keep track of a specific number of meaningful trailing spaces then you will need to store your a actual length in a separate field.

 

data test;
  length chlen 8 chstr $50 ;
  input chlen 1-5 chstr $varying50. chlen ;
cards;
00009  Leading
00010Trailing
00005Short
;

data want ;
  length newlen 8 new $200 ;
  retain newlen 0 new ;
  set test;
  new = substrn(new,1,newlen)||chstr ;
  newlen + chlen ;
  put '|' new $varying200. newlen '|';
run;
 71         data want ;
 72           length newlen 8 new $200 ;
 73           retain newlen 0 new ;
 74           set test;
 75           /*if newlen=0 then new=chstr;
 76           else */new = substrn(new,1,newlen)||chstr ;
 77           newlen + chlen ;
 78           put '|' new $varying200. newlen '|';
 79         run;
 
 |  Leading|
 |  LeadingTrailing  |
 |  LeadingTrailing  Short|
 NOTE: There were 3 observations read from the data set WORK.TEST.

View solution in original post

25 REPLIES 25
gamotte
Rhodochrosite | Level 12

Hello,

 

If you want to keep the blanks, don't use the trim function.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

concatenate=cat(concatenate,value);

See the help documentation on all of the concat functions available:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257060.htm

chithra
Quartz | Level 8

when using cat(concatenate,value) I am getting null value.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You asked to keep trailing spaces.  That is what it is doing.  A text string is is text padded out to the length of the variable with spaces.  Therefore if you have concatenate variable set as 200, that is 200 spaces.  if value is length 200, and has 4 characters then when you concatenate the variable concatenate, only the first 200 characters are stored, which as that is the first variable is the whole data.

Why not start by postings some test data in the form of a datastep (so we can run it), and what you want out.  Is likeli you want a list of variables delimited by a space, so maybe:

data want;
  set have;
  by id;
  want=ifc(first.id,strip(value),catx(' ',want,value));
  if last.id;
run;

This takes the text from value in the first occurence, and concatenates trimmed want, a space, and trimmed value from there after.

chithra
Quartz | Level 8

Many Thanks for your explanation.

 

I need to concatenate the 3 rows exactly. not include any delimeters in between.

you can see in the third macro(below) :

MEASURES3= sum_ins_SURPLUS1 cnt_ins_SURPLUS2 sum_ins_SURPLUS2 cnt_ins_FACPROP sum_ins_FACPROP cnt_ins_OTHER sum_ins_OTHER

There is a leading space. So, during concatenation this should also be there.

Also, maybe in some case there maybe trailing blanks as well, that case also.

 

hope this helps

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data, your example does not show the required things.  All text fields have trailing blanks up to the length of the variable - look at this example:

data want;
  length first $10 second $10 final $21;
  first="abc";
  second="def";
  final=cat(first,'-',second);
run;

What you will see is that I specify the text of first as being "abc", but it is stored as "abc          ", because its length is 10.  Normally you would need to trim off the additional spaces, but I can't tell from that data wether there should be one space after that text, or 2, or 7?  This also shows why presenting test data in the form of a datastep so that we can run it is very important to understanding what you have.  Follow this post if your having difficulties:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

chithra
Quartz | Level 8

data a;

input a $1-6 b $7-12;

datalines;

 

apple rose

apple lotus

;

run;

proc sort data=a;

by a;

quit;

data macros(drop=b);

length concatenate $100;

do until (last.a);

set a;

by a;

 

concatenate = cat(concatenate,b);

end;

run;

 

The above is a sample program where cat function is not working for me.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This will give you a list of all values in b concatenated by a space, all you need to do is add if last.a to get only this record:

data a;
  length a b $10;
  input a $ b $;
datalines;
apple rose
apple lotus
;
run;

proc sort data=a;
  by a;
quit;

data macros (drop=b /*rename=(concatenate=b)*/);
  length concatenate $100;
  retain concatenate;
  set a;
  by a;
  concatenate=ifc(first.a,strip(b),catx(' ',concatenate,b));
run;

Note how I set length on the input variables, and this is less that the 100, so both concatenated are length 20 which will fit in the 100 output variable.

chithra
Quartz | Level 8

Yes. Thanks.

But I need a concatenated string without space. :(Smiley Sad

RW9
Diamond | Level 26 RW9
Diamond | Level 26
  concatenate=ifc(first.a,strip(b),catx(' ',concatenate,b));

To:

  concatenate=ifc(first.a,strip(b),cat(concatenate,b));

 String functions explained:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257060.htm

 

chithra
Quartz | Level 8

concatenate=ifc(first.a,strip(b),cat(concatenate,b))

 

This is not working

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Cats():

data a;
  length a b $10;
  input a $ b $;
datalines;
apple rose
apple lotus
;
run;

proc sort data=a;
  by a;
quit;

data macros (drop=b /*rename=(concatenate=b)*/);
  length concatenate $100;
  retain concatenate;
  set a;
  by a;
  concatenate=ifc(first.a,strip(b),cats(concatenate,b));
run;
chithra
Quartz | Level 8

Thanks. But cats will remove leading and trailing blanks.

In my case, leading and trailing blanks will come. So, I don't want to remove that, I need that as well

Satish_Parida
Lapis Lazuli | Level 10

The following is working, tested. No Leading or trailing blanks removed.

 

data have;
MEASURES1='cnt_pol_COMBINE sum_pol_COMBINE cnt_pol_QA sum_pol_QA cnt_pol_FO sum_pol_FO cnt_pol_FACNP sum_pol_FACNP cnt_pol_PQS sum_pol_PQS cnt_pol_SURPLUS1 sum_pol_SURPLUS1 cnt_pol_SURPLUS2 sum_pol_SURPLUS2 cnt_';
MEASURES2='pol_FACPROP sum_pol_FACPROP cnt_pol_OTHER sum_pol_OTHER cnt_ins_COMBINE sum_ins_COMBINE cnt_ins_QA sum_ins_QA cnt_ins_FO sum_ins_FO cnt_ins_FACNP sum_ins_FACNP cnt_ins_PQS sum_ins_PQS cnt_ins_SURPLUS1';
MEASURES3=' sum_ins_SURPLUS1 cnt_ins_SURPLUS2 sum_ins_SURPLUS2 cnt_ins_FACPROP sum_ins_FACPROP cnt_ins_OTHER sum_ins_OTHER';
MEASURES4=MEASURES1||MEASURES2||MEASURES3;
run;

Please let us know if it worked for you.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 25 replies
  • 5378 views
  • 2 likes
  • 7 in conversation