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

Create an array to count the number of “other” write-in pets. Call this dataset TEMP2 and create it from TEMP1. This variable will be named OTHRPETS. Hint: Have the array count it if it is not missing (i.e., there is any pet listed in the other write-in field) and not count it if it is missing.

 

This is the code I used that adds the new variable called OTHRPETS to the table but the values underneath are not counted and just showing "."

data TEMP2; 
set temp1;
OTHRPETS = Other_spec1 + Other_spec2 + Other_spec3+ Other_spec4;
if OTHER = 1 then OTHRPETS = Other_spec1 + Other_spec2 + Other_spec3+ Other_spec4;
RUN;

saza_0-1646693797226.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You can sum numeric values. From character variables you can get a true/false 1/0 using the missing function: missing(var) will return 1 when true. So "not missing(var)" negates the result of the missing function and returns 1 when var is missing.

 

Your prof may have used a numeric example and then threw a curve.

So you could use something like:

OTHRPETS = not missing( Other_spec1) + not missing(Other_spec2) + not missing(Other_spec3) + not missing(Other_spec4);

Or, since you know there are 4 variables then the number of populated variables could be

 

OTHRPETS = 4- ( missing( Other_spec1) + missing(Other_spec2) +  missing(Other_spec3) + missing(Other_spec4) );

either of the above could use an Array to sum over the 4 variables.

If you had an array (to reduce typing) you could build a string that you could use a function like COUNTW to count the words.

Array o (*) Other_spec: ;
Otherpets = countw(catx(',', of o(*)),',');

Here the catx function places commas between values. The  Missing ones don't get added. The Countw function then counts words based on the comma separator (and returns 1 when there is only one word).

 

The "of arrayname(*)" is the way SAS says, for the functions that allow many arguments, "use all of the elements of the array". Unfortunately for counting character values there isn't are simple single function like N that returns the number of populated Numeric elements of an array.

 

View solution in original post

6 REPLIES 6
ballardw
Super User

Your question asks about array but there is no Array code shown.

 

You are adding apparently character values, what would you expect to get for "goat" + "hamster" as a numeric value? Summing is not the same as counting except under some specific instances such as when the numeric value of 1 means there is one of something. Then a sum would be the total of "somethings".

 

You might show what you want to get for desired results.

 

 

saza
Quartz | Level 8
Hmm you're right. My instructor demonstrated this way as an array however it makes sense to not be able to get an amount from a character variable. Would I use Array and then "do"
saza
Quartz | Level 8
I also tried taking your advice and now the values are all shown as 5
DATA TEMP2;
SET TEMP1;
OTHER2 = 0;
ARRAY OTHRPETS {4} Other_spec1 Other_spec2 Other_spec3 Other_spec4;
DO i = 1 TO 4;
IF OTHRPETS {i} = 1 THEN OTHER2 = OTHER2 + 1; IF OTHRPETS {i} = 0 THEN Other2 = OTHER2 + 0;
IF OTHRPETS {i}= . THEN OTHER2 = .;
END;
RUN;
ballardw
Super User

You can sum numeric values. From character variables you can get a true/false 1/0 using the missing function: missing(var) will return 1 when true. So "not missing(var)" negates the result of the missing function and returns 1 when var is missing.

 

Your prof may have used a numeric example and then threw a curve.

So you could use something like:

OTHRPETS = not missing( Other_spec1) + not missing(Other_spec2) + not missing(Other_spec3) + not missing(Other_spec4);

Or, since you know there are 4 variables then the number of populated variables could be

 

OTHRPETS = 4- ( missing( Other_spec1) + missing(Other_spec2) +  missing(Other_spec3) + missing(Other_spec4) );

either of the above could use an Array to sum over the 4 variables.

If you had an array (to reduce typing) you could build a string that you could use a function like COUNTW to count the words.

Array o (*) Other_spec: ;
Otherpets = countw(catx(',', of o(*)),',');

Here the catx function places commas between values. The  Missing ones don't get added. The Countw function then counts words based on the comma separator (and returns 1 when there is only one word).

 

The "of arrayname(*)" is the way SAS says, for the functions that allow many arguments, "use all of the elements of the array". Unfortunately for counting character values there isn't are simple single function like N that returns the number of populated Numeric elements of an array.

 

Tom
Super User Tom
Super User

You don't need to use an array.

Since you know there are 4 variables just subtract the number of missing variables from 4.

data TEMP2; 
  set temp1; 
  othrpets = 4 - cmiss(of Other_spec1-Other_spec4);
run;
saza
Quartz | Level 8
Thank you so much! that worked!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 527 views
  • 1 like
  • 3 in conversation