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; 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 961 views
  • 2 likes
  • 3 in conversation