BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ihsan-Mahdi
Quartz | Level 8

Hello,

 

I have a data set with multiple string variables, I would like to flag records with specific values AND exclude others. The values I want  include  S00-S10-S20-S30-S40-S50-S60-S70-S80-S90 only (example below). Is there a way to do that using arrays or something else? Thank you so much 🙂

Example of records wanted are in Red

 

Obs Var1 Var2 Var3 Var4 Var5 Var6 Var7
2 S05 S05 S00 S01 S00    
3 S20 S10          
4 S29 S60 S60        
6 S20 S61          
7 S16 S60 S01 S01 S01 S01 S06
10 S01 S50 S50 S00      
12 S36 S32 S40 S40      
13 S70            
14 S42 S42 S70 S80 S80 S80 S70
19 S42 S53 S40        
20 S06 S00 S60 S70      
21 S00 S40 S70        
22 S42 S40 S70        
23 S02 S02 S00 S40 S40 S40 S40
24 S32 S70 S01        
25 S42 S62 S42 S60 S80 S50 S50
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Ksharp To make this work with my environment and SAS version I also needed to explicitly define the length of the array elements.

data want;
  set have;
  array x{10} $3 _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
  array v{*} $ Var:;
  out_flg=1;
  do i=1 to dim(v);
    if not missing(v{i}) and v{i} not in: x then
      do;
        out_flg=0;
        leave;
      end;
  end;

  if out_flg then output;
  drop out_flg i;
run;

 

View solution in original post

7 REPLIES 7
Ksharp
Super User
data have;
infile cards truncover expandtabs;
input Obs	(Var1	Var2	Var3	Var4	Var5	Var6	Var7) ($);
cards;
2	S05	S05	S00	S01	S00	 	 
3	S20	S10	 	 	 	 	 
4	S29	S60	S60	 	 	 	 
6	S20	S61	 	 	 	 	 
7	S16	S60	S01	S01	S01	S01	S06
10	S01	S50	S50	S00	 	 	 
12	S36	S32	S40	S40	 	 	 
13	S70	 	 	 	 	 	 
14	S42	S42	S70	S80	S80	S80	S70
19	S42	S53	S40	 	 	 	 
20	S06	S00	S60	S70	 	 	 
21	S00	S40	S70	 	 	 	 
22	S42	S40	S70	 	 	 	 
23	S02	S02	S00	S40	S40	S40	S40
24	S32	S70	S01	 	 	 	 
25	S42	S62	S42	S60	S80	S50	S50
;

data want;
set have;
array x{10} $ _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
array v{*} $ Var:;
output=1;
do i=1 to dim(v);
 if not missing(v{i}) and v{i} not in x then do;output=0;leave;end;
end;
if output then output;
drop output i;
run;
Ihsan-Mahdi
Quartz | Level 8

Thank you! Is there a way to make _temporary_  start with the 3 character value rather than equaling it? I selected only 3 characters in my example for simplicity, what I actually have in my data are values with variable numbers of characters. I want the ones that start with the 3 I mention in my example list.

Ksharp
Super User

OK. You can change IN operator into IN:  (with colon operator).

data want;
set have;
array x{10} $ _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
array v{*} $ Var:;
output=1;
do i=1 to dim(v);
 if not missing(v{i}) and v{i} not in: x then do;output=0;leave;end;
end;
if output then output;
drop output i;
run;
Patrick
Opal | Level 21

@Ksharp To make this work with my environment and SAS version I also needed to explicitly define the length of the array elements.

data want;
  set have;
  array x{10} $3 _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
  array v{*} $ Var:;
  out_flg=1;
  do i=1 to dim(v);
    if not missing(v{i}) and v{i} not in: x then
      do;
        out_flg=0;
        leave;
      end;
  end;

  if out_flg then output;
  drop out_flg i;
run;

 

Ihsan-Mahdi
Quartz | Level 8
It worked after specifying the length!! Thank you Again for your time and brilliance 🙂
Ihsan-Mahdi
Quartz | Level 8

I'm sorry, it seems that I'm doing something wrong! Data want keeps having 0 observations!! To help clarify, the string variables in my set are named "ICD_10_dx_1 to ICD_10_dx_50".

This is the code I used based on yours:

 

data want;
set have;
array x[10] $ _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
array v[*] ICD_10_dx_1-ICD_10_dx_50;
output=1;
do i=1 to dim(v);

if not missing(v{i}) and v{i} not in: x then do;output=0;leave;end;
end;
if output then output;
drop output i;
run;

data want;
set have;
array x{10} $ _temporary_ ('S00' 'S10' 'S20' 'S30' 'S40' 'S50' 'S60' 'S70' 'S80' 'S90');
array v{*} $ ICD_10_dx_:;
output=1;
do i=1 to dim(v);
if not missing(v{i}) and v{i} not in: x then do;output=0;leave;end;
end;
if output then output;
drop output i;
run;

Where am I going wrong?! 

Patrick
Opal | Level 21

Try if adding an explicit length for the temporary array elements will resolve this issue. That's what I had to do in my environment.

Patrick_0-1726537864705.png

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 538 views
  • 4 likes
  • 3 in conversation