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:
Observationtype | Observationnummer | Var1 | Var2 | Var3 |
---|---|---|---|---|
DIF | 1 | -20 | 0 | -10 |
DIF | 2 | 100 | 0 | 30 |
DIF | 3 | 50 | 0 | -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:
Observationtype | Header 2 | Var1 | Var2 | Var3 |
---|---|---|---|---|
DIF | 1 | 0 | 0 | 0 |
DIF | 2 | 1 | 0 | 1 |
DIF | 3 | 1 | 0 | 0 |
Many many thanks again for the help!
BR Dingdang
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
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;
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
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
thanks, the "drop i" is good.
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.