DATA Step, Macro, Functions and more

Position of the variables in the order that I want

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Position of the variables in the order that I want

How can I put variables in the positions I require using SAS code?
when I use a merge, variables are positioned by default
In the dataset I can move columns / variables and change them posicision, but how to do it directly by code?


Accepted Solutions
Solution
‎10-04-2016 07:05 PM
Trusted Advisor
Posts: 1,553

Re: Position of the variables in the order that I want

Posted in reply to Angel_Saenz

you can do it by:

 

data want;

      retain  ... variable names in desired order ...;

  set have;

      ... your code ...

run;

View solution in original post


All Replies
Solution
‎10-04-2016 07:05 PM
Trusted Advisor
Posts: 1,553

Re: Position of the variables in the order that I want

Posted in reply to Angel_Saenz

you can do it by:

 

data want;

      retain  ... variable names in desired order ...;

  set have;

      ... your code ...

run;

Super User
Posts: 7,758

Re: Position of the variables in the order that I want

Posted in reply to Angel_Saenz

An "empty" format statement also works (only variable names, but no formats). Consider this if you want to prevent the main effect that retain has on variables that are not supplied by the contributing datasets.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,553

Re: Position of the variables in the order that I want

Posted in reply to KurtBremser

@KurtBremser you are right, I have tested it without and with a given format after the variables list

and it worked fine in both cases.

 

It worked only if the format statement was before the set:

 

data want;

       format x1-x5  3. ;

   set have;

run; 

Super User
Posts: 7,758

Re: Position of the variables in the order that I want

I used the term "empty" format statement as that does not override the attributes of variables coming from datasets, if those need to be preserved.

 

And yes, any "variable-ordering" statement has to come first in the data step. Doing it after the set (eg) means that the original order in the contributing datasets will take precedence. It's just how the data step compiler creates the PDV while scanning the data step code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 289 views
  • 0 likes
  • 3 in conversation