I have a sql server 2005 database that has a table with a column of data type bit. When I look at the data in sql server management studio I see the column value as 0 or 1, when i pull with SAS I see 0 or -1, is like SAS is negating the 1 value. Anyone have an explanation for this? Thanks.
SQL Server bit data type is an integer data type that can take only one of these values: 0, 1, NULL. With regard to the storage, if there are less than 9 columns of the bit data in the table, they are stored as 1 byte. If there are 9 to 16 such columns, they consume 2 bytes and so on.
Overview of BIT data type SQL Server BIT data type is an integer data type that can take a value of 0, 1, or NULL . The following illustrates the syntax of the BIT data type: BIT. SQL Server optimizes storage of BIT columns. If a table has 8 or fewer bit columns, SQL Server stores them as 1 byte.
Because SQL Server is ODBC compliant, you can use the SAS ODBC driver to read from and write to SQL Server data from within a SAS application. The driver requires an ODBC DSN on the computer running SAS pointing at the SQL Server data source. The DSN can be a user, system, or file type.
I reckon you must be using libname oledb
to connect to SQL Server from SAS. I'm able to replicate your problem here:-
SQL Server code to generate dummy data
create table dbo.tbl (
tblId int identity(1,1) not null
constraint pk_tbl_tblId primary key,
bool bit not null,
)
go
insert into dbo.tbl(bool) values(0)
insert into dbo.tbl(bool) values(1)
SAS code using OLEDB
libname imm oledb provider=sqloledb
properties=(
"Integrated Security"=SSPI
"Persist Security Info"=False
"Initial Catalog"=test
"Data Source"=localhost
);
proc print data=imm.tbl; run;
The print out is:-
Obs tblId bool
1 1 0
2 2 -1
SAS code using PROC SQL
It seems like using PROC SQL should fix your problem.
proc sql noprint;
connect to sqlservr (
server='localhost'
database='test'
'Integrated Security'='SSPI'
'Persist Security Info'='False'
);
create table test as
select *
from connection to sqlservr (
select * from dbo.tbl
);
disconnect from sqlservr;
quit;
proc print data=test; run;
The print out is:-
Obs tblId bool
1 1 0
2 2 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With