BookmarkSubscribeRSS Feed
pawandh
Fluorite | Level 6

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.

 

5 REPLIES 5
JBerry
Quartz | Level 8

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

pawandh
Fluorite | Level 6

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?

Reeza
Super User

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. 

ChrisNZ
Tourmaline | Level 20

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

PGStats
Opal | Level 21
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1326 views
  • 5 likes
  • 5 in conversation