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;
Use i instead of _n_ to index the array.
_n_ is the number of the current data step iteration.
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
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.