Hello,
I'm using an existing code template to convert sas data to xpt; however, when i run the code i get an error message. Please help!!!!
error message:
sas code:
%macro sas2xpt (sasdir=, xptdir=);
x "cd &sasdir";
filename files pipe "dir /b *.sas7bdat";
data _null_;
infile files;
length fname $ 200;
retain i 0;
input fname;
i = i + 1;
call symput("f"||left(put(i,7.)), trim(left(scan(fname,1,'.'))));
call symput("fcnt", trim(left(i)));
run;
%put &f1;
libname insas "&sasdir";
%macro dothis;
%do i = 1 %to &fcnt;
libname tranfile xport "&xptdir\&&f&i...xpt";
proc copy in=insas out=tranfile memtype=data;
select &&f&i;
run;
%end;
%mend dothis;
%dothis;
%mend sas2xpt;
**example toc all the macro**;
%sas2xpt(sasdir=%str(\\itsu.com\\Analysis\Working_Folder\ads), xptdir=(\\itsu.com\\Analysis\Working_Folder\ads) );
run;
folder structure:
So the issue does not appear to be the conversion to XPORT files. Instead it is the attempt to force WINDOWS to use a UNC path as the current working directory. That is not possible. And not needed.
There are plenty of ways to get the list of files in a directory. You can even use pure SAS code. See %dirtree()
Or you could just make a small change to your current code:
data _null_;
infile "dir /b &sasdir\*.sas7bdat" pipe truncover;
input fname $200. ;
call symputx(cats('f',_n_),scan(fname,1,'.'));
call symputx("fcnt",_n_);
run;
Also you are passing () around the value of the target directory in your macro call, which will cause the wrong filenames to be generated. Did you intend to wrap that path in a %STR() macro function call also? Note that the %STR() is not needed for the example paths you are showing since they do not include any macro triggers that need masking.
%sas2xpt
(sasdir=\\itsu.com\Analysis\Working_Folder\ads
,xptdir=\\itsu.com\Analysis\Working_Folder\ads
);
The following code is what I wrote for one to one converting to xpt file of V5.
libname in v9 'c:\temp\SAS' access=readonly; /*the path of sas datasets*/
%let path_out= c:\temp\XPT ; /*the path of xpt after converting*/
/*one dataset corresponding to one xpt file*/
data _null_;
set sashelp.vtable(keep=libname memname where=(libname='IN'));
call execute(cat("libname tranfile xport '&path_out\",lowcase(strip(memname)),".xpt';proc copy in=in out=tranfile ; select ",strip(memname),";run;"));
run;
The issue is:
You would have to use something like powershell in your pipe command. The default cmd.exe simply doesn't support UNC paths. This is a Microsoft thing and nothing you can change.
But... There is really no need to use an external command which requires XCMD to list files and especially SAS tables. Just use an approach along the line of what @Ksharp already proposed.
If you want to use your current code as-is then you need to map this UNC path (=give it a drive letter) on the machine where SAS executes.
So the issue does not appear to be the conversion to XPORT files. Instead it is the attempt to force WINDOWS to use a UNC path as the current working directory. That is not possible. And not needed.
There are plenty of ways to get the list of files in a directory. You can even use pure SAS code. See %dirtree()
Or you could just make a small change to your current code:
data _null_;
infile "dir /b &sasdir\*.sas7bdat" pipe truncover;
input fname $200. ;
call symputx(cats('f',_n_),scan(fname,1,'.'));
call symputx("fcnt",_n_);
run;
Also you are passing () around the value of the target directory in your macro call, which will cause the wrong filenames to be generated. Did you intend to wrap that path in a %STR() macro function call also? Note that the %STR() is not needed for the example paths you are showing since they do not include any macro triggers that need masking.
%sas2xpt
(sasdir=\\itsu.com\Analysis\Working_Folder\ads
,xptdir=\\itsu.com\Analysis\Working_Folder\ads
);
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.
Ready to level-up your skills? Choose your own adventure.