I'm using SAS studio. The environment is Windows 10. I would to change the dataset name of an xpt file. Anyone, could you give me the code to rename the dataset?
Thank you!
Renaming a dataset within the XPORT library is not possible, because of the internal sequential structure of the XPORT file.
You need to recreate the whole file:
/* create the xpt file for testing */
libname xp xport '/folders/myfolders/class.xpt';
proc copy in=sashelp out=xp;
select class air;
run;
libname xp clear;
/* how to rename a dataset */
/* assign xport library */
libname xp xport '/folders/myfolders/class.xpt';
/* create a temporary library within the WORK path */
%let wk = %sysfunc(pathname(WORK));
%let rc = %sysfunc(dcreate(temp,&wk.));
libname scratch "&wk./temp";
/* copy the contents there */
proc copy in=xp out=scratch;
run;
/* rename the dataset */
proc datasets lib=scratch nolist;
change class=class1;
quit;
/* move it back to the xport library */
proc copy in=scratch out=xp;
run;
/* deassign */
libname xp clear;
/* remove the scratch library */
/* empty it */
proc datasets library=scratch kill nolist;
quit;
libname scratch clear;
/* remove the directory */
%let rc=%sysfunc(filename(dref,&wk./temp));
%let rc=%sysfunc(fdelete(&dref.));
Can you post the code reading the xpt-file?
Here is my code
libname XP xport "/home/masato-shimura0/mAbprogram/02-17/serumaj.xpt";
proc copy in=XP out=A; run;
You can use proc datasets with the change-statement to rename a dataset.
So you do not want to rename a dataset within the xpt library, but the xpt file itself?
Then use the RENAME function:
libname xp xport '/folders/myfolders/class.xpt';
data xp.class;
set sashelp.class;
run;
libname xp clear;
data _null_;
rc = rename("/folders/myfolders/class.xpt","/folders/myfolders/xxx.xpt","file");
put rc=;
run;
You do NOT store a .xpt file in a library. A SAS library is for storing datasets, views, catalogs, not for external files.
With the XPORT engine, you can treat a transport file like a SAS library, but with limitations: since it has a sequential structure, it needs to be rebuilt completely when data is written to it.
I used SASHELP.CLASS only as source data for creating an example xpt file.
Renaming a dataset within the XPORT library is not possible, because of the internal sequential structure of the XPORT file.
You need to recreate the whole file:
/* create the xpt file for testing */
libname xp xport '/folders/myfolders/class.xpt';
proc copy in=sashelp out=xp;
select class air;
run;
libname xp clear;
/* how to rename a dataset */
/* assign xport library */
libname xp xport '/folders/myfolders/class.xpt';
/* create a temporary library within the WORK path */
%let wk = %sysfunc(pathname(WORK));
%let rc = %sysfunc(dcreate(temp,&wk.));
libname scratch "&wk./temp";
/* copy the contents there */
proc copy in=xp out=scratch;
run;
/* rename the dataset */
proc datasets lib=scratch nolist;
change class=class1;
quit;
/* move it back to the xport library */
proc copy in=scratch out=xp;
run;
/* deassign */
libname xp clear;
/* remove the scratch library */
/* empty it */
proc datasets library=scratch kill nolist;
quit;
libname scratch clear;
/* remove the directory */
%let rc=%sysfunc(filename(dref,&wk./temp));
%let rc=%sysfunc(fdelete(&dref.));
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.