BookmarkSubscribeRSS Feed
podarum
Quartz | Level 8

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

10 REPLIES 10
Haikuo
Onyx | Level 15

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

Reeza
Super User

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?

podarum
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

podarum
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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

Ksharp
Super User

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

Tom
Super User Tom
Super User

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.

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1061 views
  • 2 likes
  • 6 in conversation