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

Hi,

I used proc compare to compare the values of two data sets and put the differences into a dataset work.comp. So work.comp looks like this:

ObservationtypeObservationnummerVar1Var2Var3
DIF1-200-10
DIF2100030
DIF3500-10

I want to update this table with the following rules: for all the variables, if the observation is bigger than 0, then replace the observationvalue with '1' else replace with '0'.

the example above will then look like this:

ObservationtypeHeader 2Var1Var2Var3
DIF1000
DIF2101
DIF3100

Many many thanks again for the help!

BR  Dingdang

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

alternatively, if you have many variables to change then arrays will be a good option

data want;

    set have;

    array vars(*) var1 - var3;

    do i =1 to dim(vars);

    if vars(i) > 0 then vars(i)=1;

    else vars(i)=0;

    end;

run;

Thanks,

jagadish

Thanks,
Jag

View solution in original post

7 REPLIES 7
pradeepalankar
Obsidian | Level 7

data work.comp;

set work.comp ;

if var1<=0 then var1=0 ;else var1=1;

if var2<=0 then var2=0 ;else var2=1;

if var3<=0 then var3=0 ;else var3=1;

;run;

proc print;run;

Jagadishkatam
Amethyst | Level 16

alternatively, if you have many variables to change then arrays will be a good option

data want;

    set have;

    array vars(*) var1 - var3;

    do i =1 to dim(vars);

    if vars(i) > 0 then vars(i)=1;

    else vars(i)=0;

    end;

run;

Thanks,

jagadish

Thanks,
Jag
Dingdang
Fluorite | Level 6

hi Jagadish,

this is exactly what i am looking for. I wrote sth. similar but i used {} instead of () to address the elements in the array. I think I need to look into the differences between () [] and {}.

Thanks again. BR  Dingdang

Amir
PROC Star

Hi,

A slight variation on the response supplied by :

data want;

  set have;

  drop i;

  array vars(*) var1 - var3;

  do i =1 to dim(vars);

    vars(i)=vars(i) > 0;

  end;

run;

Regards,

Amir.

Dingdang
Fluorite | Level 6

thanks, the "drop i" is good.

Amir
PROC Star

Hi,

Yes, the drop is good, but, in case it was not noticed, the main change was the absence of any if-then-else statement.

Regards,

Amir.

Dingdang
Fluorite | Level 6

Hey Amir,

hehe, thanks for pointing that out. as you correctly noticed, i didnt notice that Smiley Happy learned sth. new again, have to look into that when i finish the current task...

have a great day.  Dingdang

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 2418 views
  • 7 likes
  • 4 in conversation