concentrating two variables into same variable

Reply
Contributor
Posts: 34

concentrating two variables into same variable

data have;

JoBC='FA';

JoBL='1';

JoBC= JoBC || JoBL ;

run;

want

JoBC      JoBL

FA1          1



Super User
Super User
Posts: 9,599

Re: concentrating two variables into same variable

Posted in reply to anitapamu2

Hi,

I suspect that what you are missing is variable length.  As you do not specify how long JoBC is it is defaulted to 2 characters after the first assignment.  When you concat on the "1" that makes 3 chars and the last is dropped.  Always be explicit in your definitions!

data have;

  length JoBC $20;

  JoBC='FA';

  JoBL='1';

  JoBC=strip(JoBC)||strip(JoBL);

run;

Valued Guide
Posts: 864

Re: concentrating two variables into same variable

Posted in reply to anitapamu2

It won't work in this case because the variable is the same name but for future reference you can use this:

new_var = cats(var1,var2,varn...);

Respected Advisor
Posts: 3,167

Re: concentrating two variables into same variable

Posted in reply to anitapamu2

Your question happens to tap on one of the fundamental features of SAS. SAS, specifically speaking 'data step', only supports fixed length variable as a programming language. For your purpose, you will have to either give a long enough length for a variable upfront or not using 'data step'.

data have;

     var1='al';

     var2='abc';

run;

/*length of var1 is predefined by the incoming dataset, length = 2*/

data data_step_fail;

     set have;

     var1=cats(var1,var2);

run;

/*length of var1 is redefined as 20*/

data data_step_success;

     length var1 $ 20;

     set have;

     var1=cats(var1,var2);

run;

/*SQL works on completely different mechanism*/

proc sql;

     create table want_sql as select cats(var1,var2) as var1, var2 from have;

quit;

Super User
Posts: 6,781

Re: concentrating two variables into same variable

Posted in reply to anitapamu2

As previously noted, there can be issues with lengths of variables.  The easiest way to program around this might be to rename:

data want;

   set have;

   newvar = JoBC || JoBL;

   drop JoBC;

   rename newvar = JoBC;

run;


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