Concatenation of numeric vars turning char?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Concatenation of numeric vars turning char?

Hello:

I have concatenated three numeric variables into one variable called cell, however, my created variable is character instead of numeric.  Would we know why this is happening?

The three variables I'm concatenating are numeric, default length 8 but the variable cell is character, length 32.   I used the following code:

data drive.t10;

    set t9;

format child_grd_preschl_numb child_grade_numb. child_grd_kto5_numb child_grade_numb. child_grd_6to12_numb child_grade_numb.;

cell=child_grd_preschl_numb||child_grd_kto5_numb||child_grd_6to12_numb;

where the child_grade_numb. numeric format is as follows:


proc format;

value child_grade_numb

        0="0"

        1="1"

        2-10="2+";

run;

Is there something I could do instead of converting the variable cell to numeric in an additional statement?

Thank you very much for your feedback,


Accepted Solutions
Solution
‎12-30-2014 03:50 PM
Super User
Posts: 11,144

Re: Concatenation of numeric vars turning char?

Concatenation by definition is pretty much a character operation. SAS is likely using a BEST format for some of the variables and you are likely getting blanks in the result to equal the length of the formats used

Are you going to do any arithmetic operations on the resulting variable? If not, what's the problem with a character result? How are you going to use the cell variable?

Also you may be thankful for the blanks if you are trying to do what I think you want:

Suppose;

child_grd_preschl_numb= 31

child_grd_kto5_numb = 5

child_grd_6to12_numb = 6

Are you wanting 3156 as a result?

If so consider

child_grd_preschl_numb= 3

child_grd_kto5_numb = 15

child_grd_6to12_numb = 6

Same result

If you really need a NUMERIC result one approach would be to multiply each variable by an appropriate value and add them together: child_grd_preschl_numb*1000 + child_grd_kto5_numb*100 + child_grd_6to12_numb

Provide some example data in the form of data step and desired outcomes.

View solution in original post


All Replies
Contributor
Posts: 45

Re: Concatenation of numeric vars turning char?

Hi,

It looks like the variables of interest might be character to start and you are tyring to use a format to turn them into numeric? Is that right?

Nonetheless, I don't believe you can concatenate numeric variables anyway.

Maybe you can tell us a little more about what you hope to do with the CELL variable.

Thanks,

M.

SAS Employee
Posts: 340

Re: Concatenation of numeric vars turning char?

data step converts automatically numeric values to character if the context requires. Concatenation is an operator that requires both operands to be character. In the log you shoud see a note about that.

Now the question is, how do you want to aggregate ("concatenate") three numeric values into one?

Please describe what should be the result of different scenarios.

e.g.: 0 || 0 || 1 = 1 ?

Solution
‎12-30-2014 03:50 PM
Super User
Posts: 11,144

Re: Concatenation of numeric vars turning char?

Concatenation by definition is pretty much a character operation. SAS is likely using a BEST format for some of the variables and you are likely getting blanks in the result to equal the length of the formats used

Are you going to do any arithmetic operations on the resulting variable? If not, what's the problem with a character result? How are you going to use the cell variable?

Also you may be thankful for the blanks if you are trying to do what I think you want:

Suppose;

child_grd_preschl_numb= 31

child_grd_kto5_numb = 5

child_grd_6to12_numb = 6

Are you wanting 3156 as a result?

If so consider

child_grd_preschl_numb= 3

child_grd_kto5_numb = 15

child_grd_6to12_numb = 6

Same result

If you really need a NUMERIC result one approach would be to multiply each variable by an appropriate value and add them together: child_grd_preschl_numb*1000 + child_grd_kto5_numb*100 + child_grd_6to12_numb

Provide some example data in the form of data step and desired outcomes.

Contributor
Posts: 45

Re: Concatenation of numeric vars turning char?

While we wait to find out how you plan to use the CELL variable, here is the link to the SAS documentaiton about "Working with Character Variables" which includes a discussion about concenation: Step-by-Step Programming with Base SAS(R) Software

Frequent Contributor
Posts: 131

Re: Concatenation of numeric vars turning char?

Thanks to each of you!

I have the cell variable in two data sets (called rates0 and t10, respectively) which I'd like to merge BY the variable cell.  I don't need the cell variable as numeric; I can keep it as character.  However, since my data sets did not merge (code below), I thought the problem could be due to the cell variables being character but having different lengths/spacing issues and therefore not matching between the two data sets.

data c0;

merge rates0 (in=rates_) t10 (in=t10_);

     by cell;

if rates_=1 and t10_=1 then output c0;

/*call rantbl(seed, p1, p2, p3, pick1);*/

run;

In proc freq, the valid values for the two data sets' cell vars look diff and I'm unsure why.  For the rates0 dataset, I made this a SAS dataset from an Excel where cell's valid values use '(apostrophes), i.e. '110, '120, etc.  For the t10 dataset, I had concatenated three numeric variables to make cell.

Thank you so much

Contributor
Posts: 45

Re: Concatenation of numeric vars turning char?

When you concatenate, the the length of the new variable will be the sum of the lengths of the contributing variables. You will need to make sure the values for the variables in the BY statement are exactly the same. To start, you can use a LENGTH statement to specify the length of the new variable (CELL) and then TRIM the contributing variables, i.e. removing the trailing blanks. The documentation in my previous response goes over this in some detail and should be helpful.

Hope that helps,

M.

SAS Employee
Posts: 340

Re: Concatenation of numeric vars turning char?

You marked the question as answered, but now I think the right answere for you is:

cell=put(child_grd_preschl_numb, child_grade_numb.)||put(child_grd_kto5_numb, child_grade_numb.)||put(child_grd_6to12_numb, child_grade_numb.);

I used the put() function to explicitely convert numbers (grades) to the three-level encoding (0-1-2). To simply use the format statment is not enough, if you want to concateneate the formated values.

Also, to match your codes in the Excel, rather define the format like this:

proc format;

value child_grade_numb

        0="0"

        1="1"

        2-10="2";/*removed the + sign*/

run;

Super User
Posts: 11,144

Re: Concatenation of numeric vars turning char?

If the ONLY use for Cell is to merge by then it isn't needed. You can merge by more than one variable on a by statement. Sort the data by all three first and have at it.

Frequent Contributor
Posts: 131

Re: Concatenation of numeric vars turning char?

Hi ballardw,

Thanks so much!  Since it turns out I actually need this CELL var to use in IF-THEN statements ( ie if cell="000" etc)

do you know why when i concatenate the numeric vars to create cell but then specify something like cell="0 0 0 " or cell="000"

why it doesn't recognize these values for cell?

Super User
Posts: 11,144

Re: Concatenation of numeric vars turning char?

It may be time to show us the results of the created Cell causing issues, and possibly a log entry to see if you are getting invalid numeric data messages.

A numeric value of 0 will not match anything with imbedded spaces as "0 0" will not be converted to numeric.

A numeric value of zero does match when I do cell="000" or with leading and/or trailing spaces.

Super User
Posts: 19,194

Re: Concatenation of numeric vars turning char?

Change your if/then statements.

Respected Advisor
Posts: 3,156

Re: Concatenation of numeric vars turning char?

Like others already pointed out, Concatenation is for Chars only. Therefore, unless you are not doing concatenation, you will have to convert it back to number one way or another. A quick and dirty way of conversion is done by SAS automatically:

data have;

a=2;b=3;c=4;

d=cats(a,b,c)+0;

run;

Haikuo

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 1142 views
  • 6 likes
  • 6 in conversation