Help with Combine Character variable and numerical variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

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

Thank you for your suggestion.

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

View solution in original post


All Replies
Solution
‎11-06-2013 09:32 PM
Frequent Contributor
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: 371

Re: Help with Combine Character variable and numerical variable

Thank you so much, Mit.

HHC

Super Contributor
Posts: 371

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.

If you know how to fix it, please help me.

Thank you,

HHC

Respected Advisor
Posts: 4,659

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

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

Respected Advisor
Posts: 4,659

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
Respected Advisor
Posts: 4,659

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.

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

Discussion stats
  • 7 replies
  • 281 views
  • 9 likes
  • 3 in conversation