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: 7,720

Re: concentrating two variables into same variable

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: 858

Re: concentrating two variables into same variable

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,156

Re: concentrating two variables into same variable

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: 5,365

Re: concentrating two variables into same variable

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
  • 277 views
  • 0 likes
  • 5 in conversation