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

Hi Team,

I need help in replacing the position of old variable with the new variable. 

 

 

Example: 

DATA NEW(DROP=make);
SET SASHELP.cars;
DROP TYPE Horsepower;
Type_new='All';
Horsepower_new=1000;
RENAME Type_new=Type Horsepower_new=Horsepower;
RUN;

In the above example: 

The new variables are positioned at the end of the dataset. But, I want these variables to be in the same position i.e., at the original horsepower and type position(which are dropped in the data step).

How do we do that in the same data step?

Please note that the drop variables (data set option) and the new variable creation would be changing depending on the requirement. This should be dynamic in a way. 

 

Thank you,

Prad

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you are "recalculating" value perhaps just set them to missing and reuse the old names. Change the LABEL to reflect the change if needed.

 

DATA NEW(DROP=make);
   SET SASHELP.cars;
   call missing( TYPE, Horsepower);
   Type='All';
   Horsepower=1000;
RUN;

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

I have to say that changing the order of variables in a SAS data set is usually unnecessary. Regardless of your intended use, the data set itself doesn't have to have variables in any particular order. PROCs such as PROC PRINT and PROC TABULATE and PROC REPORT allow the order of the variables to be set easily when you use these PROCs.

--
Paige Miller
Reeza
Super User
How do you know which new variable matches with which old variable?
Is the naming convention shown here something you can rely on?
prad001
Obsidian | Level 7
The new variables will have an "_new" extension.
ballardw
Super User

If you are "recalculating" value perhaps just set them to missing and reuse the old names. Change the LABEL to reflect the change if needed.

 

DATA NEW(DROP=make);
   SET SASHELP.cars;
   call missing( TYPE, Horsepower);
   Type='All';
   Horsepower=1000;
RUN;

 

Shmuel
Garnet | Level 18

As you assigning new values to variables in ALL observations, you don't need to assign missing values to them. Anyhow yoy can control order of variables to show when you open the dataset by using FORMAT statement with or without a format, as in:

 

DATA NEW(DROP=make);
  FORMAT <1st_var> <2nd_var> <3rd_var> ... ;
   SET SASHELP.cars;
   Type='All';
   Horsepower=1000;
RUN;
prad001
Obsidian | Level 7
OMG.. This is exactly what I am looking for. Thank you Ballard.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 511 views
  • 1 like
  • 5 in conversation