BookmarkSubscribeRSS Feed
saza
Quartz | Level 8

So the dataset that i'm using needs a new variable that is the addition of people who have other types of pets aside from cats and dogs. The answers are not numeric but rather character variables. So in the do feature I listed the character variables but I get a synthax error.

data Temp2; 
set Temp1;
array OTHRPETS {4} Other_spec1 Other_spec2 Other_spec3 Other_spec4;
do i = goat hamster lizard chicken rabbit gerbil iguana spider pig ferret;
if OTHRPETS {i} = . then OTHRPETS {i} = . ;
end;
run;

 

saza_0-1646707556282.png

 

1 REPLY 1
Tom
Super User Tom
Super User

You have an answer on your other thread.

https://communities.sas.com/t5/SAS-Programming/Using-ARRAY/m-p/800774/highlight/true#M315073

 

Let's look at these statements one by one.

array OTHRPETS {4} Other_spec1 Other_spec2 Other_spec3 Other_spec4; 

So this says you want to make an array named OTHRPETS (spelled without the first E) that refers to 4 variables.   So far so good, but it could be a lot simpler.  You don't have to tell SAS how many variables are in the array if you are going to list them.  You can use a variable list since your variables have a common prefix and a continuous series of numeric suffices.

array OTHRPETS Other_spec1-Other_spec4; 

Next you have this DO statement. 

do i = goat hamster lizard chicken rabbit gerbil iguana spider pig ferret;  

As the error message says that is not a valid form of the DO statement.  If you want to use a list of values then they have to be separated by commas. So this would be valid syntax:

do i = goat, hamster, lizard, chicken, rabbit, gerbil, iguana, spider, pig, ferret;  

Even if it makes no sense for the problem at hand.  It would mean to run the DO loop 10 times, with I taking the value of one of the other 10 variables on each iteration of the DO loop.

The next statement is valid syntax, but also does not make much sense.

if OTHRPETS {i} = . then OTHRPETS {i} = . ;  

So you are saying if the i'th variable in the list of variables used by OTHRPETS is missing the set it to missing.  So it does nothing.  But since you are comparing the variable to the numeric missing value instead of character missing (a string that only has blanks) it could cause trouble.  SAS will have to try to convert the character value to a number to do the comparison.  It will have to convert the missing numeric value to a character value to do the assignment.

Consider this example:

1     data test;
2       set sashelp.class;
3       if name=. then name=.;
4     run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      3:6
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      3:23
NOTE: Invalid numeric data, Name='Alfred' , at line 3 column 6.
Name=. Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=1 _N_=1
NOTE: Invalid numeric data, Name='Alice' , at line 3 column 6.
Name=. Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=1 _N_=2

Which will force NAME to be a right aligned period  on every observation.

                                     Cumulative    Cumulative
Name        Frequency     Percent     Frequency      Percent
-------------------------------------------------------------
       .          19      100.00            19       100.00

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!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 652 views
  • 1 like
  • 2 in conversation