Solved
Contributor
Posts: 58

# modify all the observations of all the variables in a dataset

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:

DIF1000
DIF2101
DIF3100

Many many thanks again for the help!

BR  Dingdang

Accepted Solutions
Solution
‎11-14-2013 05:16 AM
Posts: 1,147

## Re: modify all the observations of all the variables in a dataset

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,

Thanks,
Jag

All Replies
Frequent Contributor
Posts: 106

## Re: modify all the observations of all the variables in a dataset

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;

Solution
‎11-14-2013 05:16 AM
Posts: 1,147

## Re: modify all the observations of all the variables in a dataset

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,

Thanks,
Jag
Contributor
Posts: 58

## Re: modify all the observations of all the variables in a dataset

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

Super Contributor
Posts: 339

## Re: modify all the observations of all the variables in a dataset

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.

Contributor
Posts: 58

## Re: modify all the observations of all the variables in a dataset

thanks, the "drop i" is good.

Super Contributor
Posts: 339

## Re: modify all the observations of all the variables in a dataset

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.

Contributor
Posts: 58

## Re: modify all the observations of all the variables in a dataset

Hey Amir,

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

have a great day.  Dingdang

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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