sas - Merge Specifying Column Order -
i have 2 sas data sets similar this:
dataone datatwo ----------- ------------------ id c e id b d f 1 x x x 1 x x x 2 x x x 2 x x x 3 x x x 3 x x x
they're of equal length row-wise , have same sorted values id in each column. initially, combine data went following simple one-to-one merge statement:
data combined; merge dataone datatwo; run;
this produces output follows:
combined -------------------- id c e b d f 1 x x x x x x 2 x x x x x x 3 x x x x x x
this has of data want, columns in wrong order. ideal output reorganize columns this:
combinedtwo -------------------- id b c d e f 1 x x x x x x 2 x x x x x x 3 x x x x x x
however, i'm not sure how reorganize columns that. checking sas website there called match-merge seems used merging rows correctly (in case there missing lines or something). point me in right direction appreciated.
note: i'm pretty complete beginner sas having used few days apologies if simple question , missed answer when searching.
first off, want match-merge:
data combined; merge dataone datatwo; id; run;
a 1 1 merge risks problems if a) 1 dataset not sorted way think should be, or b) 1 dataset missing 1 or more ids. match merge makes sure ids match. require datasets sorted prior merge.
secondly, cannot automatically reorder variable names directly. need somehow construct ordered list of variable names, depending on how want order them.
one way construct list , use in retain statement. statement else, convenient use reordering variables.
data combined; retain id b c d e f; merge dataone datatwo; id; run;
that okay, nice move out of data step , have somewhere can edit if changes more easily, if need use in several places. 1 way handle put in macro variable:
%let varorder = b c d e f;
then can use so:
data combined; retain id &varorder.; merge dataone datatwo; id; run;
note leave id out of it, needs front in event. that'll important later.
now, works short example, might impractical larger datasets. how grabbing order datasets?
proc contents data=dataone out=var_dataone(keep=name); run; proc contents data=datatwo out=var_datatwo(keep=name); run; data vars; set var_dataone var_datatwo; name; if name='id' delete; run;
okay, have dataset our names, in proper order. can pull macro variable few different ways; here's one.
proc sql; select name :varorder separated ' ' vars order name; quit;
now have &varorder
constructed datasets' contents. it's in alphabetical order; if wanted in other order you'd want reorder how like. use varnum
, on contents output (add keep
statement) if want try , keep in order on dataset.
Comments
Post a Comment