BookmarkSubscribeRSS Feed
imabition_gmail_com
Calcite | Level 5

 

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;

 

 

 

 

5 REPLIES 5
imabition_gmail_com
Calcite | Level 5
Yes this works. Thanks
Kurt_Bremser
Super User

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

 

 

Reeza
Super User

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; 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1676 views
  • 2 likes
  • 3 in conversation