BookmarkSubscribeRSS Feed
supersoup
Calcite | Level 5

I am writing a program that gets IDs from a database then outputs them in files 900 at a time.  The IDs need to be surrounded by apostrophes and comma separated.  The list needs to start with an open parentheses and end with a closed parentheses in each of 3 files.  For example:

FileA.txt

('11111',

'22222',

'33333')

FileB.txt

('44444',

'55555',

'66666')

Here is my attempt at doing this, but I am a beginner and am sure this is not the best way.  And cannot figure out how to get rid of the final comma.

%Let taxlength=900;

data _null_;

file out1;

put @1 "(";

file out2;

put @1 "(";

file out3;

put @1 "(";

run;

data _null_;

set v_id;

if _n_ <= &taxlength then file out1 mod notitles ;

if 901 <= _n_ <= (&taxlength*2) then file out2 mod notitles ;

if 1801 <= _n_ <= (&taxlength*3) then file out3 mod notitles ;

vendorid=vendorid;

put @1 "'"

@2 vendorid 10.

@12 "'"

@13 ",";

run;

data _null_;

file out1 mod;

put @1 ")";

file out2 mod;

put @1 ")";

file out3 mod;

put @1 ")";

run;

3 REPLIES 3
Tom
Super User Tom
Super User

You can use the FILEVAR option to generate the target filename.  You can also place your SET statement inside of an iterative DO loop to enforce the maximum file length.

I have coded this to place the commas at the beginning of the lines rather than the end of the lines as I find it much easier to read.

* Set the maximum into a macro variable to make it easier ;

%let max=15;

* Generate some dummy data to test ;

data dummy;

do _n_=1 to  3*&max - 10;

  vendorid=int(1e10*ranuni(0));

  output;

end;

run;

data _null_;

  filenum+1;

  filename=cats('out',filenum,'.txt');

  file dummy filevar=filename ;

  put '(' @ ;

  do _n_=1 to &max while (not eof);

     set dummy end=eof;

     if _N_>1 then put ',' @;

     put "'" vendorid +(-1) "'" ;

  end;

  put ')';

  if eof then stop;

run;


*** OUT1.TXT *** ;

('6112930255'

,'4017536623'

,'4614022222'

,'6636999275'

,'4235494129'

,'8338204831'

,'5500628452'

,'912050027'

,'4984888660'

,'3984991583'

,'1706911647'

,'4505465866'

,'7472367876'

,'2576759510'

,'6629737469'

)


supersoup
Calcite | Level 5

Fantastic! Worked like a charm.  One other thing; how can I email each of these files?

MikeZdeb
Rhodochrosite | Level 12

hi ... here's one idea with 30 observations to be written three files, 10 vendorids per file ...

* data set with 30 observations;

data v_id;

do _n_=1 to 30;

   vendorid = put(ceil(1e5*ranuni(999)),z5.);

   output;

end;

run;

data _null_;

* three file names;

array fn(3) $20 _temporary_ ('z:\one.txt' 'z:\two.txt' 'z:\thr.txt');

do i=1 to 3;

* filevar does not allow array variables;

  fname=fn(i);

  file x filevar=fname;

  put '(' @;

  do j=1 to 10;

    set v_id;

    if j lt 10 then put vendorid quote.;

    else put vendorid quote. ')';

  end;

end;

stop;

run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 1022 views
  • 0 likes
  • 3 in conversation