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

Hi,

 

I have multiple sas datsets(~30) present in a directory and need to convert all these into text files.  I am using the code below for same:

=====================================================

LIBNAME Test "D:\";

%MACRO Convert2CSV(Libname);

DATA MEMBERS;
SET SASHELP.VMEMBER(WHERE=(LIBNAME = "&Libname"));

RETAIN OBS 0;
OBS = OBS+1;

KEEP MEMNAME OBS;

RUN;

PROC SQL;
SELECT MIN(OBS) INTO :MIN
FROM MEMBERS;
QUIT;

PROC SQL;
SELECT MAX(OBS) INTO :MAX
FROM MEMBERS;
QUIT;

%Local D;

%DO D = &MIN %TO &MAX;

PROC SQL;

SELECT COMPRESS(MEMNAME) INTO: Table
FROM MEMBERS
WHERE OBS=&D;

QUIT;

PROC EXPORT DATA=&Libname..&Table DBMS=dlm replace
OUTFILE="D:\_today\&Table..txt";
delimiter="|";
RUN;

%END;

%MEND;

%Convert2CSV(TEST);

==================================================================

Note: I have leveraged this code from (https://communities.sas.com/t5/SAS-Programming/Export-multiple-tables-from-SAS-to-csv/td-p/27090)

 

Issue: The generated text files have spaces in the name. For eg: 'myfile.sas7bat' gets converted to 'myfile    .txt'

Just a hunch but I think the generated filenames are of fixed length. Therefore to meet the length, SAS is introducing spaces. How do avoid this?

 

Ask: I need help in removing the spaces from the generated file name. I have tried using the Trim function but it doesn't seems to be working.

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

I forgot to add the libname in my call execute. Expand the call execute like this.

call execute("
  proc export
    data=test." || strip(memname) || "
    dbms=dlm
    replace
    outfile='D:\_today\" || strip(lowcase(memname)) || ".txt'
  ;
  delimiter='|';
  run;
";

Note that I also changed the usage of single and double quotes, to facilitate the use of a macro variable/parameter if you later want to wrap the whole data step into a macro definition for easier reuse.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Why don't you just do it in one data step that creates the exports with call execute?

data _null_;
set sashelp.vmember (where=(libname = "TEST"));
call execute('
  proc export
    data=' || strip(memname) || '
    dbms=dlm
    replace
    outfile="D:\_today\' || strip(lowcase(memname)) || '.txt"
  ;
  delimiter="|";
  run;
';
run;
sg16773
Calcite | Level 5

Hi KurtBremser,

 

Thanks for your response. I tried the code you suggested but I am getting the following error(screenshot below). May be the code is not able to search the dataset in the defined library("TEST").  

 

Apologies for the silly questions but I am very new to SAS and trying to learn the language.

 

Capture.PNG

Kurt_Bremser
Super User

I forgot to add the libname in my call execute. Expand the call execute like this.

call execute("
  proc export
    data=test." || strip(memname) || "
    dbms=dlm
    replace
    outfile='D:\_today\" || strip(lowcase(memname)) || ".txt'
  ;
  delimiter='|';
  run;
";

Note that I also changed the usage of single and double quotes, to facilitate the use of a macro variable/parameter if you later want to wrap the whole data step into a macro definition for easier reuse.

sg16773
Calcite | Level 5
Thanks KurtBremser!

This did the trick 🙂
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3046 views
  • 0 likes
  • 2 in conversation