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

Hi everyone,

 

I have a dataset that roughly looks like the one below.

 

data have;
 infile cards delimiter='|';
input X1 $ V1 $ V2 $ V3 $;
cards;
	|a1|	|a1	|
	||c2,a1|	c3,c2	|
	||b2|		|
te	||	|	|
;
run;

Now I want to create a new column miss that indicates whether or not a row had any observation in V1, V2, or V3.

Row 1, 2 and 3 all have values in either V1, V2, V3 or some combination, so miss would here be 1.

Row 4 has no values in V1-V3, so miss would be 0. 

Important is that the solution is dynamic, since the dataset grows on a frequent basis. 

 

I think an additional issue for my data is that i'm not sure these empty spaces are actually flagged as missing? How would I go about checking that? i've tried some of the missing functions in SAS, but i find them so unintuitive that I might as well just be doing it wrong and getting confusing results.

 

Thanks a bunch.

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The statement 

 

  miss=(cats(of v1-v3)^=' ');

assigns a 1 to MISS if the expression cats(of v1-v3)^=' ' is true, or a 0 otherwise.  The CATS function concatenates the characters of V1, V2, and V3 (while stripping all leading or trailing blanks from the components).   If all of them are missing (i.e. blank) then the CATS result is blank, making the expression true.  So

 

data have;
 infile cards delimiter='|';
input X1 $ V1 $ V2 $ V3 $;
cards;
	|a1|	|a1	|
	||c2,a1|	c3,c2	|
	||b2|		|
te	||	|	|
;
run;
data want;
  set have;
  miss=cats(of v1-v3)^=' ';
run;

will do what you want.

--------------------------
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

--------------------------

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Use the CMISS function to count the number of missings.

--
Paige Miller
cmues
Fluorite | Level 6

Sorry, I really do not understand at all how to apply these functions to create a column or even a summary. Nor do I feel the documentation is very clear on it. Is it part of a DATA or PROC? 

PaigeMiller
Diamond | Level 26

@cmues wrote:

Sorry, I really do not understand at all how to apply these functions to create a column or even a summary. Nor do I feel the documentation is very clear on it. Is it part of a DATA or PROC? 


In a data step

 

count=cmiss(of var1-var3);
--
Paige Miller
mkeintz
PROC Star

The statement 

 

  miss=(cats(of v1-v3)^=' ');

assigns a 1 to MISS if the expression cats(of v1-v3)^=' ' is true, or a 0 otherwise.  The CATS function concatenates the characters of V1, V2, and V3 (while stripping all leading or trailing blanks from the components).   If all of them are missing (i.e. blank) then the CATS result is blank, making the expression true.  So

 

data have;
 infile cards delimiter='|';
input X1 $ V1 $ V2 $ V3 $;
cards;
	|a1|	|a1	|
	||c2,a1|	c3,c2	|
	||b2|		|
te	||	|	|
;
run;
data want;
  set have;
  miss=cats(of v1-v3)^=' ';
run;

will do what you want.

--------------------------
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

--------------------------

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2206 views
  • 2 likes
  • 3 in conversation