BookmarkSubscribeRSS Feed
R_A_G_
Calcite | Level 5

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;

8 REPLIES 8
R_A_G_
Calcite | Level 5

just a general question:

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

Thank you

Haikuo
Onyx | Level 15

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

R_A_G_
Calcite | Level 5

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

Haikuo
Onyx | Level 15

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

Linlin
Lapis Lazuli | Level 10

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

R_A_G_
Calcite | Level 5

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;

Linlin
Lapis Lazuli | Level 10

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

Linlin

R_A_G_
Calcite | Level 5

Thank you

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1763 views
  • 0 likes
  • 3 in conversation