BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Angel_Saenz
Quartz | Level 8

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

you can do it by:

 

data want;

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

  set have;

      ... your code ...

run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

you can do it by:

 

data want;

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

  set have;

      ... your code ...

run;

Kurt_Bremser
Super User

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.

Shmuel
Garnet | Level 18

@Kurt_Bremser 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; 

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1780 views
  • 0 likes
  • 3 in conversation