Help using Base SAS procedures

keep variable at the back

Reply
Super Contributor
Posts: 395

keep variable at the back

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

Respected Advisor
Posts: 3,124

Re: keep variable at the back

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

Super User
Posts: 17,863

Re: keep variable at the back

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?

Super Contributor
Posts: 395

Re: keep variable at the back

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..?

Respected Advisor
Posts: 3,124

Re: keep variable at the back

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

Super Contributor
Posts: 395

Re: keep variable at the back

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

Respected Advisor
Posts: 3,124

Re: keep variable at the back

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

Respected Advisor
Posts: 3,124

Re: keep variable at the back

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

Super User
Posts: 9,682

Re: keep variable at the back

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

Super User
Super User
Posts: 6,502

Re: keep variable at the back

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.

Respected Advisor
Posts: 4,651

Re: keep variable at the back

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

PG
Ask a Question
Discussion stats
  • 10 replies
  • 316 views
  • 2 likes
  • 6 in conversation