How to merge the two ASCII files in SAS?

Reply
Occasional Contributor M_K
Occasional Contributor
Posts: 14

How to merge the two ASCII files in SAS?

hi all,

I wanted to know if there is any command/ program in SAS which helps to merge two ASCII files by some variable, say serial no.?

Say for example, in the following two ascii set-ups first column represents serial no. and rest of the columns have some other variables:

ASCII file 1:

01  2  9  1

02  5  8  2

03  6  2  1

04  2  1  2

05  1  1  2

ASCII file 2:

01  12  29  81

02  45  58  32

03  16  22  41

04  62  21  32

05  11  41  92

I want to get the following ASCII file with the following set-up after merging:

01  2  9  1  12  29  81

02  5  8  2  45  58  32

03  6  2  1  16  22  41

04  2  1  2  62  21  32

05  1  1  2  11  41  92

Please let me know if it is possible. Thanks!

Super Contributor
Posts: 1,636

Re: How to merge the two ASCII files in SAS?

data one;
input id $2. v1-v3;
cards;
01  2  9  1
02  5  8  2
03  6  2  1
04  2  1  2
05  1  1  2
;
data two;
input id $2.v4-v6;
cards;
01  12  29  81
02  45  58  32
03  16  22  41
04  62  21  32
05  11  41  92
;
data three;
merge one two;
by id;
run;

proc print;run;
                            obs    id    v1    v2    v3    v4    v5    v6

                             1     01     2     9     1    12    29    81
                             2     02     5     8     2    45    58    32
                             3     03     6     2     1    16    22    41
                             4     04     2     1     2    62    21    32
                             5     05     1     1     2    11    41    92

Respected Advisor
Posts: 3,156

Re: How to merge the two ASCII files in SAS?

I am sure there will be slicker approaches, but for now, please try the following tested codes:

filename f1 'h:\temp\f1.txt';

filename f2 'h:\temp\f2.txt';

filename merg 'h:\temp\merge.txt';

data f1;

  infile f1;

  input id$ x1-x3; output;

  run;

data f2;

infile f2;input id$ y1-y3; output ;

run;

proc sort data=f1 out=f1;

by id;

run;

proc sort data=f2 out=f2;

by id;

run;

data _null_;

file merg;

  merge f1 f2;

  by id;

put id x1--y3;

run;

Note: change folders to match your running settings.

Regards,

Haikuo

Ask a Question
Discussion stats
  • 2 replies
  • 342 views
  • 3 likes
  • 3 in conversation