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
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;
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.
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.