DATA Step, Macro, Functions and more

concatenate variables with out removing leading and trailing spaces

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

concatenate variables with out removing leading and trailing spaces

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;


Accepted Solutions
Solution
‎02-16-2018 02:35 AM
Super User
Super User
Posts: 7,860

Re: concatenate variables with out removing leading and trailing spaces

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


All Replies
Super Contributor
Posts: 320

Re: concatenate variables with out removing leading and trailing spaces

[ Edited ]

Hello,

 

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

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

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

Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

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

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

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.

Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

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

 

 

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

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

Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

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.

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

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.

Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

Yes. Thanks.

But I need a concatenated string without space. Smiley SadSmiley Sad

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

  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

 

Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

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

 

This is not working

Super User
Super User
Posts: 9,227

Re: concatenate variables with out removing leading and trailing spaces

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;
Contributor
Posts: 28

Re: concatenate variables with out removing leading and trailing spaces

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

Frequent Contributor
Posts: 109

Re: concatenate variables with out removing leading and trailing spaces

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 24 replies
  • 179 views
  • 2 likes
  • 6 in conversation