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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.