BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
capam
Pyrite | Level 9

Hi,

 

I'm trying to use 'band' to solve the following. bitwise AND operator on up to 6 digit binary inputs.

 

The syntax I'm using is:


data have;
set have;
output = input(put(band(input1,input2),binary6.),6.);
run;


The current output is:
input1 input2 output
111111 100      100
111111 1001    1
111111 101111 111
111111 111111 111
111110 100000 0

 

It should be:
input1 input2   output
111111 100      100
111111 1001    1001
111111 101111 101111
111111 111111  111111
111110 100000 100000

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@capam wrote:
Case 2 produces correct output when followed by a format:
format output binary6.;
How do I remove the extra zeros on the left. Thanks so much.

I think you refer to my earlier post where I outlined the two cases. The "decimal output" solution in the more recent post removes the leading zeros by converting the binary numbers to decimal numbers, e.g., the binary 4 (100 in the binary system) is converted to the decimal 100 (one hundred). Alternatively, you could define output as a character variable if it's only used for reporting purposes:

 

/* Character output */

data want;
set have;
output = put(input(put(band(input(put(input1,6.),binary.),input(put(input2,6.),binary.)),binary6.),6.),6.);
run;

 

View solution in original post

13 REPLIES 13
FreelanceReinh
Jade | Level 19

Hi @capam,

 

How do you create the original dataset HAVE?

 

Please see below how your code works correctly with my dataset HAVE:

data have;
input (input1 input2) (:binary.);
format input: binary6.;
cards;
111111 100
111111 1001
111111 101111
111111 111111
111110 100000
;

data have;
set have;
output = input(put(band(input1,input2),binary6.),6.);
run;

Result:

input1    input2    output

111111    000100       100
111111    001001      1001
111111    101111    101111
111111    111111    111111
111110    100000    100000
capam
Pyrite | Level 9
I do no processing on it. It is read in directly from a data base.
I saw your post before, however, it didn't work with these inputs.
FreelanceReinh
Jade | Level 19

@capam wrote:
I do no processing on it. It is read in directly from a data base.


Sure, you read the data with SAS. But there are different ways to read data and many mistakes that one can make.

Please show your code.

capam
Pyrite | Level 9

proc sql;
create table WS_data as
SELECT
FLT.*,
CCA.*,
CCA2.*

FROM RMDEOAP.GETS_DW_EOA_FAULTS FLT
full join RMDEOAP.GETS_DW_EOA_MP_AC_CCA CCA on (FLT.fault_objid=CCA.PARM2FAULT)
full join RMDEOAP.GETS_DW_EOA_MP_AC_CCA2 CCA2 on (FLT.fault_objid=CCA2.PARM2FAULT)
WHERE
FLT.vehicle_header='CN'
and Wheel_Diameter_1 is not null
and FLT.FAULT_CODE = '20-0001'

and FLT.occur_date between '01JAN2015:00:00:00'dt and datetime()
order by Unit
;
quit;

FreelanceReinh
Jade | Level 19

Thanks for posting the code. So, most likely your input1, input2 variables contain either character values like '101101' (which are automatically converted to numeric values when you apply the BAND function to them) or decimal numeric values like 101101. For a solution it's important to know: Are they numeric or character?

 

Edit: The solutions for the two cases differ in the arguments of the BAND function:

 

Case 1: character

band(input(left(input1),binary.),input(left(input2),binary.))

The LEFT function can be omitted if the values are already left-aligned or if they have a length <=8.

 

Case 2: numeric

band(input(put(input1,6.),binary.),input(put(input2,6.),binary.))

 

capam
Pyrite | Level 9
numeric
capam
Pyrite | Level 9
It may be important to note that the 1st 3 output digits are correct counting from right to left. The last 3 digits are left out.
FreelanceReinh
Jade | Level 19

Thanks. So, this example should match your situation:

data have;
input input1 input2;
cards;
111111 100
111111 1001
111111 101111
111111 111111
111110 100000
;

/* Binary output */

data want;
set have;
output = band(input(put(input1,6.),binary.),input(put(input2,6.),binary.));
format output binary6.;
run;

/* Decimal output (not suitable for use as binary values!) */

data want;
set have;
output = input(put(band(input(put(input1,6.),binary.),input(put(input2,6.),binary.)),binary6.),6.);
run;
capam
Pyrite | Level 9
Case 2 produces correct output when followed by a format:
format output binary6.;
How do I remove the extra zeros on the left. Thanks so much.
FreelanceReinh
Jade | Level 19

@capam wrote:
Case 2 produces correct output when followed by a format:
format output binary6.;
How do I remove the extra zeros on the left. Thanks so much.

I think you refer to my earlier post where I outlined the two cases. The "decimal output" solution in the more recent post removes the leading zeros by converting the binary numbers to decimal numbers, e.g., the binary 4 (100 in the binary system) is converted to the decimal 100 (one hundred). Alternatively, you could define output as a character variable if it's only used for reporting purposes:

 

/* Character output */

data want;
set have;
output = put(input(put(band(input(put(input1,6.),binary.),input(put(input2,6.),binary.)),binary6.),6.),6.);
run;

 

ballardw
Super User

Try this:

data have;
   informat input1 input2 binary6. ;
   input input1 input2;
   output = band(input1,input2);
   format output binary6.;
datalines;
111111 100   
111111 1001  
111111 101111
111111 111111
111110 100000
;

When you use a an INPUT with 6. format you told it not to treat values as binary any more.

 

capam
Pyrite | Level 9
I get an error ERROR: No DATALINES or INFILE statement.
ballardw
Super User

@capam wrote:
I get an error ERROR: No DATALINES or INFILE statement.

Show your code and error messages from the Log. Paste into a code box opened with the {I] icon here.

 

Bet you missed a ; somewhere.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 13 replies
  • 2360 views
  • 0 likes
  • 3 in conversation