Concatenating String

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Concatenating String

HI,

I'm trying to concatenate a group of strings stored in an array. Here's the code:

data _null_ ;

  array fileList[100] $ 256 _temporary_;

  fileList[1] = "1 ";

  fileList[2] = "2 ";

  fileList[3] = "3 ";

  fileList[4] = "4 ";

  temp = "";

  do i = 1 to 4;

       temp = cat(temp, fileList);

       put temp;

  end;

run;

This does not work. "put temp" prints blanks. Any ideas?


Accepted Solutions
Solution
‎10-05-2013 12:28 AM
Super User
Posts: 19,870

Re: Concatenating String

Do you want them to one dataset or multiple datasets? If one dataset you can use the wildcard to read them in and the filename option to get the filename.

data want;

length my_file file_name $256.;

infile 'this is my path/*.csv' filename=my_file;

file_name=my_file;

input .......;

run;

There's also a ton of macros on line to do this...is there something particular about your method that you like?

http://www.sascommunity.org/wiki/Filelist_macro

http://snipplr.com/view/54424/

SAS FAQ: How do I read multiple raw data files with the same structure in one data step?

View solution in original post


All Replies
Super User
Posts: 19,870

Re: Concatenating String

Do you really want to loop?

Does the following get you closer?

data _null_ ;

length temp $256;

  array fileList[100] $ 256 _temporary_;

  fileList[1] = "1 ";

  fileList[2] = "2 ";

  fileList[3] = "3 ";

  fileList[4] = "4 ";

  temp = catx(" ", of filelist(*));

    put temp;

run;

Respected Advisor
Posts: 4,934

Re: Concatenating String

Try this:


data test ;
  length temp $8;
  array fileList[100] $ 256 _temporary_;
  fileList[1] = "1 ";
  fileList[2] = "2 ";
  fileList[3] = "3 ";
  fileList[4] = "4 ";
  temp = "";

  do i = 1 to 4;
       temp = catx(" ", temp, fileList);
       put temp  =;
  end;
run;

or better:

data test ;

  length temp $8;

  array fileList[100] $ 256 _temporary_;

  fileList[1] = "1 ";

  fileList[2] = "2 ";

  fileList[3] = "3 ";

  fileList[4] = "4 ";

  temp = catx(" ", of fileList{*});

  put temp=;

run;

PG

PG
Occasional Contributor
Posts: 6

Re: Concatenating String

If i wanted the result to look like this:

temp =

'1' '2' '3' '4'

what might that code look like? Specific, what I'm doing is I'm scanning a directory for a set of files and then I want to read all those files into a dataset. So I'm reading the whole list of files from the directory, finding the right filenames that I want, saving it into an array, and trying to do something like:

filename csvfiles ('filename1' 'filename2' 'filename3');

data tempdata;

     infile csvfiles;

     ... etc

run;

Am I doing it the right way?

  infile csvfiles

Super User
Posts: 19,870

Re: Concatenating String

There are better ways to do that. Post what you're trying to do instead.

Occasional Contributor
Posts: 6

Re: Concatenating String

data dataFileNames (keep=filename year month day);

       array fileList[100] $ 256 _temporary_;

       arraySize = 0;

       arrayWasTrunc = 0;

       length temp $5000;

       * read files in a directory and story it in an array;

       call readFilesInDirectory("/unixdirectory/", 0, fileList, arraySize, arrayWasTrunc);

       if arrayWasTrunc then put 'ERROR: Not enough result array entries. Increase array size.';

      csv_file_list_string = "";

       do i = 1 to arraySize;

          if (substr(fileList,1,26) = "SOMEPREFIX_") then do

            year  = substr(fileList,27,4);    

            month = substr(fileList,31,2);    

            day   = substr(fileList,33,2);

            if ((month >= &REPORT_START_MONTH.) and (month <= &REPORT_END_MONTH.) and

                (year >= &REPORT_START_YEAR.) and (year <= &REPORT_END_YEAR.)) then do

                 filename = fileList;

                 output;    

              end;

          end;

    end;

run;

--------

At this point I'm trying to read all these CSV files that are stored in the filename dataset.

Solution
‎10-05-2013 12:28 AM
Super User
Posts: 19,870

Re: Concatenating String

Do you want them to one dataset or multiple datasets? If one dataset you can use the wildcard to read them in and the filename option to get the filename.

data want;

length my_file file_name $256.;

infile 'this is my path/*.csv' filename=my_file;

file_name=my_file;

input .......;

run;

There's also a ton of macros on line to do this...is there something particular about your method that you like?

http://www.sascommunity.org/wiki/Filelist_macro

http://snipplr.com/view/54424/

SAS FAQ: How do I read multiple raw data files with the same structure in one data step?

Respected Advisor
Posts: 3,799

Re: Concatenating String

after creating work.datafilenames I would use that data to drive a data step that uses FILEVAR= INFILE statement option.

Example 5: Reading from Multiple Input Files

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 320 views
  • 5 likes
  • 4 in conversation