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;
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.
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.
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.
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;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: