## Help with Combine Character variable and numerical variable

Solved
Super Contributor
Posts: 506

# Help with Combine Character variable and numerical variable

Hello Everyone,

I have a data with 1 character (var) and 1 number (value) variables.

I want to combine to make a new variable in the final form: var1=value.

so the new column in my data look like

acb=1

xyz=-1

bb=2

c=1

HHC

data have;

input var1 \$ value;

datalines;

abc 1

xyz -1

bb 2

c 1

;run;

Accepted Solutions
Solution
‎11-06-2013 09:32 PM
Frequent Contributor
Posts: 83

## Re: Help with Combine Character variable and numerical variable

data have;

input var1 \$ value;

datalines;
abc 1
xyz -1
bb 2
c 1
;run;

data want;
set have;
newvar=compbl(var1||'='||value);
run;

 var1 value newvar abc 1 abc = 1 xyz -1 xyz = -1 bb 2 bb = 2 c 1 c = 1

All Replies
Solution
‎11-06-2013 09:32 PM
Frequent Contributor
Posts: 83

## Re: Help with Combine Character variable and numerical variable

data have;

input var1 \$ value;

datalines;
abc 1
xyz -1
bb 2
c 1
;run;

data want;
set have;
newvar=compbl(var1||'='||value);
run;

 var1 value newvar abc 1 abc = 1 xyz -1 xyz = -1 bb 2 bb = 2 c 1 c = 1
Super Contributor
Posts: 506

## Re: Help with Combine Character variable and numerical variable

Thank you so much, Mit.

HHC

Super Contributor
Posts: 506

## Re: Help with Combine Character variable and numerical variable

By the way, when I combine the variables, the length of new variable become too long (about 40 character) and it doesn't fit 1 row.

I try to change Length even to \$800 but still SAS create 2 row in 1 cell for that variable.

Thank you,

HHC

Posts: 5,526

## Re: Help with Combine Character variable and numerical variable

The dataset variable doesn't span two lines, it's just the way it is displayed. Make the column wider and you will see it is a single line. - PG

PG
Super Contributor
Posts: 506

## Re: Help with Combine Character variable and numerical variable

PGStats,

Is there anyway to specify the width of column in the code so that it will not overflow into 2 line?

Thank you,

HHC

Posts: 5,526

## Re: Help with Combine Character variable and numerical variable

Yes, use either

proc print data=want width=full noobs; run;

or

proc sql noflow; select * from want; quit;

PG

PG
Posts: 5,526

## Re: Help with Combine Character variable and numerical variable

Or, as the catx function is exactly made for that purpose :

data have;

input var1 \$ value;

datalines;

abc 1

xyz -1

bb 2

c 1

;

data want;

set have;

length newVar \$16;

newVar = catx("=", var1, value);

run;

PG

PG
🔒 This topic is solved and locked.