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