Hi, does anyone know how I can keep a variable to the back if using merge, set or proc means or proc transpose...I knwo retain keeps it in formation, but how can I always keep a new variable added to the back.. thanks
Your question is too general to be answered. It seems to me that you want a variable to be retained, if that is the case, in data step you can use
1. retain statement
2. sum statement
3. DOW.
You maybe need to lay out some detailed scenario for specific answer.
Regards,
Haikuo
Do you mean add it to the end of a data set, so that if your current data set has two variables
ID Var1
and you add a new variable Var2 your data should be
ID Var1 Var2?
I mean if I have this code:
proc transpose data=Test out=Test2 (drop=_name_ _label_) suffix=_Price;
by City Store;var Price;id Date;run;
It stores the Store right in the front, but how can I put the Store right at the back behind all the Prices..?
If I understand you correctly, you would need anther additional step (eg. data step) to get what you need.
data want;
retain blah1 blah2 .... col1 col2... STORE;
set test2;
run;
Regards,
Haikuo
I have tried that and it still leaves STORE at the front.. Let me put it another way..
If I have a dataset of STORE, Var1, Var2, Var3 -- Var20 .. How Can I make it to look like this
Var1, Var2, Var3 -- Var20, STORE .. thanks
It seems working for me, try this, make sure 'retain' statement runs before 'set'.
data want;
retain var1-var20 STORE;
set test2;
run;
Regards,
Haikuo
And if you don't want to spell out your variable names if there are many, try to use metadata:
proc sql;
select name into :name separated by ' ' from dictionary.columns
where libname='WORK' AND MEMNAME='TEST2' AND NAME NE 'STORE'; quit;
data want;
retain &name STORE;
set TEST2;
run;
Regards,
Haikuo
You can always keep it as the last variable with defining it as the last statement.
data class(drop=_age);
set sashelp.class(rename=(age=_age));
age=_age;
run;
Ksharp
Here is an easy way using DROP dataset option.
data want ;
set have(obs=0 drop=store) have;
run;
You can use multiples of these zero obs datasets with combinations of keep and drop options to reorder variables.
Tom, your mastery of the datastep keeps amazing me!
But at the same time, I feel somewhat annoyed that the simplest way to do certain things with SAS is to use side effects. Long time ago, when I learned to program, I was warned about relying on side effects to achieve my goals. They are often underdocumented and always poorly understood by newcomers. I wonder why the theme of variable ordering keeps coming back, as I am sure it has been for years, without SAS including some feature in the language to address it.
Excuse the rant.
PG
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.