DATA Step, Macro, Functions and more

Create variables using values across the records

Reply
Occasional Contributor
Posts: 5

Create variables using values across the records

[ Edited ]

 

data have;

infile cards dlm='09'x truncover;

data have;

infile cards dlm='09'x truncover;

input id            a          b          c;

cards;

1          2          2          1

2          3          1          0

3          1          0          4

4          1          2          3

5          3          2          1

;

run;

 

 

want:

 

id         a          b          c       g1     g2    g3    g4

1          2          2          1       1       1      0     0

2          3          1          0       1       1     1     0

3          1          0          4       1       1     1     1

4          1          2          3       1       1     1     0

5          3          2          1       1        1     1    0

 

So,

If a--c is >=1 then g1=1

If a--c is >=2 then g2=1

If a--c is >=3 then g3=1

If a--c is >=4 then g4=1

 

code:

data want;
set have;
array input(*) a--c;
do i=1 to dim(input);
if input{_n_} ge 1 then g1=1;
if input{_n_} ge 2 then g2=1;
if input{_n_} ge 3 then g3=1;
if input{_n_} ge 4 then g4=1;
end;
run;

 

 

 

 

Super User
Posts: 6,932

Re: Create variables using values across the records

Use i instead of _n_ to index the array.

_n_ is the number of the current data step iteration.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Create variables using values across the records

Yes this works. Thanks
Super User
Posts: 6,932

Re: Create variables using values across the records

Since you might want to later expand the groups, here's dynamic code:

data have;
input id a b c;
cards;
1 2 2 1
2 3 1 0
3 1 0 4
4 1 2 3
5 3 2 1
;
run;



%macro execute(maxgroups);
data want;
set have;
array vars(*) a--c;
%do i = 1 %to &maxgroups.;
g&i. = 0;
%end;
do i=1 to dim(vars);
%do i = 1 %to &maxgroups.;
  if vars{i} ge &i. then g&i. = 1;
%end;
end;
drop i;
run;
%mend;
%execute(4)
proc print noobs;run;

Result:

id    a    b    c    g1    g2    g3    g4

 1    2    2    1     1     1     0     0
 2    3    1    0     1     1     1     0
 3    1    0    4     1     1     1     1
 4    1    2    3     1     1     1     0
 5    3    2    1     1     1     1     0

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,932

Re: Create variables using values across the records

Have you actually taken one look at the log of your step?

input cannot be used as the name for an array, as it is also the name of a standard SAS function.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,784

Re: Create variables using values across the records

Also, what does a--c mean? That doesn't have meaning in the context you're using it in. 

 

You probably want either the max( of a--c) or sum(). I'm guessing max based on context. 

I would suggest finding max and looping up to that value to set G vars to 1. 

 

Array gv(4) g1-G4 (4*0);

 

max_count = max(a,b,c);

 

do i=1 max_count;

g(i) = 1;

end; 

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