Help using Base SAS procedures

formating to binary

Reply
Regular Contributor
Posts: 161

formating to binary

Hello SAS experts,

I have a data that needs to be changed to binary, I've used formating but it seems like it only changed the way the data looks like, not chaning the value. B/C when I feed it to my other software it says the values are not binary. Help Please

this is my code:

proc format library=ut;

value chan2G

        2,1=1

        0=0

        other=0

        ;

RUN;

LIBNAME ut 'C:\Users\Roofia\Desktop\JOURNAL_JAN2012\TIMSS 2007_BOOK1\TIMSS 2007_\DATA\TWN_USA_PROGRAM';

OPTIONS FMTSEARCH=(ut);

RUN;

DATA UT.BOOK1_TWN_07;

SET ut.Bsctwnm4;

     FORMAT M022232 chan2G.  M022234A chan2G.  M022234B chan2G. M042220 chan2G.

RUN;

Regular Contributor
Posts: 161

formating to binary

just a general question:

does formatting change the actual value or does it only changes the appearance?

Thank you

Respected Advisor
Posts: 3,124

formating to binary

It only changes appearance.

You have to re-read in the values. In you case, here is an options:

proc format ;

invalue chan2G

        2,1=1

        0=0

        other=0

        ;

RUN;

data have;

input a b @@;

cards;

1 2 3 4 4 0 0 1

;

proc sql;

select name into :name  separated by ' ' from dictionary.columns

where libname='WORK' and memname='HAVE';

quit;

filename test "h:\test.txt";

data _null_;

set have;

file test;

put &name;

run;

data want;

infile test truncover;

input (&name) (:chan2G.);

output;

run;

proc print;run;

Regards,

Haikuo

Regular Contributor
Posts: 161

formating to binary

Thank you

The only problem I have is that I have never worked with SQL before and don't really know what it does.

Thank you again

Respected Advisor
Posts: 3,124

formating to binary

You can use datastep only:

data _null_;

set sashelp.vcolumn end=last;

length names $100;

retain names;

if libname='WORK' and memname='HAVE' then

names=catx(' ',names,name);

if last then call symputx('name',names);

run;

to replace SQL part of it:

  

proc sql;

select name into :name  separated by ' ' from dictionary.columns

where libname='WORK' and memname='HAVE';

quit;

Haikuo

Super Contributor
Posts: 1,636

Re: formating to binary

Is this helpful?

data have;

input M022232  M022234A  M022234B  M042220 ;

cards;

1 2 1 4

2 3 4 1

3 0 3 5

0 1 2 1

;

data want

  set have;

  array old(*) M022232  M022234A  M022234B  M042220;

  do _n_=1 to dim(old);

  if old(_n_) in (1,2) then old(_n_)=1;

     else old(_n_)=0;

   end;

run;

proc print;

run;

Obs    M022232    M022234A    M022234B    M042220

1        1           1           1          0

2        1           0           0          1

3        0           0           0          0

4        0           1           1          1

Linlin

Regular Contributor
Posts: 161

Re: formating to binary

Thank you all,

I ended u using Linlin's sugestion and also tried another way and was wondering if this way is correct as well

I used this code and was wondering if it actually changes the values.

DATA UT.BOOK1_TWN;

SET UT.BOOK1_TWN_07;

  if M022232 in (1,2) then M022232_1=1;

    else M022232_1=0;

if M022234A in (1,2) then M022234A_1=1;

    else M022234A_1=0;

if M022234B in (1,2) then M022234B_1=1;

    else M022234B_1=0;

if M042220 in (1,2) then M042220_1=1;

    else M042220_1=0;

drop M022232 M022234A M022234B M042220;

  run;

Super Contributor
Posts: 1,636

Re: formating to binary

Yes, your code will change the values and the values will be 1 or 0.

Linlin

Regular Contributor
Posts: 161

Re: formating to binary

Thank you

Ask a Question
Discussion stats
  • 8 replies
  • 371 views
  • 0 likes
  • 3 in conversation