I have a dataset like this( I'll just list a few lines, actually it might be many line such as 20 30 ..):
subjid value
001 12
002 25
003 30
004 6
004 9
.....
And I need to comparing their value each other pairwise ,so I want to output:
subjid value value_1 value_2 value_3 value_4 value_5
001 12 12 25 30 6 9
002 25 12 25 30 6 9
003 30 12 25 30 6 9
004 6 12 25 30 6 9
004 9 12 25 30 6 9
.......
Dear SAS expert,
Could you create a macro program to do that ?? many thank.
9.3... that may be the reason why it is not working. Probably, the curobs= option was introduced in 9.4.
Here is the documentation for the SET statement: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.ht...
and some example:
Curobs= creates a variable which keeps the number of currently read observation (the real observation number from the data set, so even if WHERE is applied it will return the real obs number).
In case there are no ties in your data on the VALUE variable you can use the following code (since here the _N_ imitates the curobs= quite well):
data have;
input subjid value;
format subjid z3.;
cards;
001 12
002 25
003 30
004 16
005 9
006 26
007 6
;
run;
proc sort data = have out = want;
by value;
run;
data want;
set want nobs = nobs;
compare = (_N_ - 1) - (nobs - _N_);
run;
proc print;
run;
Bart
This seems to be part of a larger task. And if so, there is definitely a better way to get there than to create columns for all of your observations.
What is the real task here?
Ok. Can you explain your problem from your sample data?
What does your desired result look like from that? Makes it much easier to provide a usable code answer 🙂
005 9 ..... And I need to comparing their value each other pairwise ,so I want to output: subjid value value_1 value_2 value_3 value_4 value_5 001 12 12 25 30 6 9 002 25 12 25 30 6 9 003 30 12 25 30 6 9 004 6 12 25 30 6 9 005 9 12 25 30 6 9 ....... Dear SAS expert, Could you create a macro program to do that ?? many thank.
@Garyho , Also, I merged your two similar questions into one 🙂
data have; input subjid value; format subjid z3.; cards; 001 12 002 25 003 30 004 6 004 9 ; proc transpose data=have out=temp prefix=value_; var value; run; data want; set have; if _n_=1 then set temp(drop=_name_); run;
Just for complete case I would add:
data want;
set have;
if _n_=1 then set temp(drop=_name_);
array V value_:;
compare = 0;
do over V;
compare + sign(value - V);
end;
run;
Bart
Just for fun, one more approach without transposing, just with sorting (and few more obs for testing "ties").
data have;
input subjid value;
format subjid z3.;
cards;
001 12
002 25
003 30
004 6
005 9
006 25
007 6
;
data want;
do _N_ = 1 by 1 until(last.value);
set want nobs = nobs curobs = curobs;
by value;
if first.value then compare = (curobs - 1);
if last.value then compare = compare - (nobs - curobs);
end;
do _N_ = 1 to _N_;
set want;
output;
end;
run;
proc print;
run;
[EDIT] if there are no ties it reduces to:
data have;
input subjid value;
format subjid z3.;
cards;
001 12
002 25
003 30
004 16
005 9
006 26
007 6
;
run;
proc sort data = have out = want;
by value;
run;
data want;
set want nobs = nobs curobs = curobs;
compare = (curobs - 1) - (nobs - curobs);
run;
proc print;
run;
Bart
9.3... that may be the reason why it is not working. Probably, the curobs= option was introduced in 9.4.
Here is the documentation for the SET statement: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.ht...
and some example:
Curobs= creates a variable which keeps the number of currently read observation (the real observation number from the data set, so even if WHERE is applied it will return the real obs number).
In case there are no ties in your data on the VALUE variable you can use the following code (since here the _N_ imitates the curobs= quite well):
data have;
input subjid value;
format subjid z3.;
cards;
001 12
002 25
003 30
004 16
005 9
006 26
007 6
;
run;
proc sort data = have out = want;
by value;
run;
data want;
set want nobs = nobs;
compare = (_N_ - 1) - (nobs - _N_);
run;
proc print;
run;
Bart
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.