DATA Step, Macro, Functions and more

code for creating file

Reply
Occasional Contributor
Posts: 15

code for creating file

hi, actually it would be great if I can have code for the following probem

I have a large no. of variable but I'm just presenting for few. suppose I have a file named A of the following form

      s    e     d   v1    v2    v3    v4   v5    v6

1    2    6     2    1

2    3    5     4    1     1                            1

3                       1     1      1      1            1

4    2    3     5    1                     1     1     1

 

In the above data file, red colored numbers are just the serial number, and blank spaces means missing variables

Now I want to put 5 in the variables starting from s=2 and ending at e=6, e.i. starting from v2 to v6, if the variable d is greater than 1. But I want the columns s & e as the index of starting and ending of entry, I don't want the incorporation of variables directly, so that I can use this programme for a very large number of variables and cases, and for that the entry of in the data file depends on the s and e variables. e.g. If s is 2 then the recoding should start from v2 and if e is 6 the recoding should end at v6. Now I want my new table looks like as follows:

 

 

      s    e     d   v1    v2    v3    v4   v5    v6

1    2    6     2    1     5     5      5     5      5

2    3    5     0    1     1                            1

3                       1     1     1      1             1

4    2    3     5    1     5     5      1     1      1

 

This means that, the previous entries in the table remains as before and just put in the required fields.

It would be of great healp if you send me the code for doing this.

 

 

Respected Advisor
Posts: 4,640

Re: code for creating file

Use an array :

 

data have;
input       
s    e     d   v1    v2    v3    v4   v5    v6;
datalines;
2    6     2    1     .     .     .    .     .
3    5     0    1     1     .     .    .     1
.    .     .    1     1     1     1    .     1
2    3     5    1     .     .     1    1     1
;

data want;
set have;
array _v{*} v:;
if d > 1 then
    do i = max(1,s) to min(dim(_v),e);
        _v{i} = 5;
        end;
drop i;
run;

proc print data=want noobs; run;
PG
Occasional Contributor
Posts: 15

Re: code for creating file

awesome...thanks a lot for the code...

Ask a Question
Discussion stats
  • 2 replies
  • 160 views
  • 1 like
  • 2 in conversation