DATA Step, Macro, Functions and more

Concatenation

Reply
Contributor
Posts: 62

Concatenation

A="FA";

B="1";

AB=AB||B;

BA=B||BA;

rub;

For BA the value is coming as . 1 but for AB it is coming as  .??It should be same for AB as well.

 

Contributor
Posts: 64

Re: Concatenation

[ Edited ]

I have no idea what you're trying to do, but you're referencing variables that don't exist:  

 

AB=AB||B   -  the program doesn't know what AB is?

 

So since you set B as a '1' but then never told it what AB was supposed to be, it automatically converts your number into a numeric 1 and then it results in a NOTE: in your log.

 

Next, you do something similar with B||BA, except B actually exists (but as a string) - so it concatenates as

'1            .'  

 

So, check your log. The answer is often there.

 

Since i dont know what you're doing, I cant help very much from here. But my big tips are:

(1) use length and input statements to correctly define the variable you're adding to avoid surprises

(2) check  your log often... even when you think things went correctly

Contributor
Posts: 62

Re: Concatenation

I am just checking how it works.I got for AB||B -here my AB is missing n if i concatenate it with B then first B which is a character will be converted to numeric(best 12 format) so i will get  ". 1";

But i don't understand how it works for B||AB .I got that it would be "1   ." ,but in my output it is a missing value?

can u explain why?

Super User
Posts: 19,783

Re: Concatenation

Use the CAT/T/X/S/Q family of functions instead. It's better at handling missing values, if that's what you're after. 

PROC Star
Posts: 1,759

Re: Concatenation

+1 for the cat functions.

The concatenate operator || doesn't trim spaces, and I suspects that's why you don't get the expected result, since the second variable in the || operation will be pushed at the very end of the first one.

Respected Advisor
Posts: 4,920

Re: Concatenation

[ Edited ]
1    data test;
2    A="FA";
3    B="1";
4    AB=AB||B;
5    BA=B||BA;
6    run;

NOTE: Numeric values have been converted to character
      values at the places given by: (Line):(Column).
      4:4   5:7
NOTE: Character values have been converted to numeric
      values at the places given by: (Line):(Column).
      4:6   5:5
NOTE: Invalid numeric data, '1           .' , at line 5 column 5.
A=FA B=1 AB=0.1 BA=. _ERROR_=1 _N_=1
NOTE: The data set WORK.TEST has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.89 seconds
      cpu time            0.07 seconds

My interpretation:

 

On line 4, SAS will set the type of new variable AB to the type of the expression on the right of the assignment.

Checking that expression, it meets the still untyped variable AB which this time gets the default type: numeric

and initial value: missing. Once set, the type of a variable cannot change.

Concatenation of AB and B thus requires the conversion of numeric AB to a string. The default format (BEST12.) is used and yields

'           .' (11 spaces and a period) . The concatenation result is '           .1' which is then assigned to AB (now typed), after conversion to numeric: 0.1

 

The same line of reasoning on line 5 yields the string '1           .' as the result of concatenation, which cannot be converted to

numeric and thus generates an Invalid numeric data note and a missing value for BA.

 

The apparent lack of symmetry between the two results comes from the fact that "           .1" can be converted to numeric

but "1           ." can't.

 

 

 

PG
Ask a Question
Discussion stats
  • 5 replies
  • 353 views
  • 5 likes
  • 5 in conversation