BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
weronikaz
Calcite | Level 5

Hello,

I have the following data set:

Student_id     Week1  Week2  Week3  Week4  Week5  Week6

123                   0             4           3           0          0            2

124                   4             3           4           4          4            4 

125                   2             0           0           3          2            1

126                   0             2           3           0          2             0

127                   0             0           0           0          0            0

0 means that the person didn't attend any classes in that week, 1/2/3 means how many classes student attended in the week. I need to produce a report with students who didn't attend any classes for at least 2 weeks in a row. Does anyone have an idea how to do it? Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @weronikaz 


data have;
input Student_id     Week1  Week2  Week3  Week4  Week5  Week6;
cards;
123                   0             4           3           0          0            2
124                   4             3           4           4          4            4 
125                   2             0           0           3          2            1
126                   0             2           3           0          2             0
127                   0             0           0           0          0            0
;

data want;
 set have;
 array t week:;
 _n_=countw(cats(of t(*)),'0','k');
 do _n_=1 to _n_;
  if lengthn(scan(cats(of t(*)),_n_,'0','k'))>=2 then do;
   output;
   leave;
  end;
 end;
run;
Student_id Week1 Week2 Week3 Week4 Week5 Week6
123 0 4 3 0 0 2
125 2 0 0 3 2 1
127 0 0 0 0 0 0

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Normally to work with a series of variables you would use an ARRAY statement to allow you use an index.

In this simple case I would just convert the values to a string and use some simple pattern matching.

data have;
  input Student_id $ Week1-Week6;
cards;
123 0 4 3 0 0 2
124 4 3 4 4 4 4
125 2 0 0 3 2 1
126 0 2 3 0 2 0
127 0 0 0 0 0 0
;

data want;
  set have;
  if indexw(catx(' ',of week1-week6),'0 0');
run;

proc print;
run;
       Student_
Obs       id       Week1    Week2    Week3    Week4    Week5    Week6

 1       123         0        4        3        0        0        2
 2       125         2        0        0        3        2        1
 3       127         0        0        0        0        0        0
mkeintz
PROC Star
just be sure no one ever attends 10 classes anytime except week 6.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

@mkeintz wrote:
just be sure no one ever attends 10 classes anytime except week 6.

That wouldn't make any difference.  Notice the use of INDEXW().

It might catch 0 followed by 0.5 but only if you include period in addition to space in list of delimiters in the INDEXW() call.

 

novinosrin
Tourmaline | Level 20

Hi @weronikaz 


data have;
input Student_id     Week1  Week2  Week3  Week4  Week5  Week6;
cards;
123                   0             4           3           0          0            2
124                   4             3           4           4          4            4 
125                   2             0           0           3          2            1
126                   0             2           3           0          2             0
127                   0             0           0           0          0            0
;

data want;
 set have;
 array t week:;
 _n_=countw(cats(of t(*)),'0','k');
 do _n_=1 to _n_;
  if lengthn(scan(cats(of t(*)),_n_,'0','k'))>=2 then do;
   output;
   leave;
  end;
 end;
run;
Student_id Week1 Week2 Week3 Week4 Week5 Week6
123 0 4 3 0 0 2
125 2 0 0 3 2 1
127 0 0 0 0 0 0
novinosrin
Tourmaline | Level 20

   


data have;
input Student_id     Week1  Week2  Week3  Week4  Week5  Week6;
cards;
123                   0             4           3           0          0            2
124                   4             3           4           4          4            4 
125                   2             0           0           3          2            1
126                   0             2           3           0          2             0
127                   0             0           0           0          0            0
;


data want;
 set have;
 if prxmatch('/(0){2,}/', cats(of week1-week6));
run;
Ksharp
Super User
data have;
input Student_id     Week1  Week2  Week3  Week4  Week5  Week6;
cards;
123                   0             4           3           0          0            2
124                   4             3           4           4          4            4 
125                   2             0           0           3          2            1
126                   0             2           3           0          2             0
127                   0             0           0           0          0            0
;
data want;
 set have;
 array x{*} week: ;
 do _n_=1 to dim(x)-1;
  if x{_n_}=0 and x{_n_+1}=0 then do;output;leave;end;
 end;
run;
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
  • 6 replies
  • 1512 views
  • 5 likes
  • 5 in conversation