DATA Step, Macro, Functions and more

simple array question

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

simple array question

New SAS user - trying to make an array that creates a new variable with observations as '1' when it finds either 9359 or 9351 (character, not numeric) in the array variables.

Here's my code

 

data cuthip;
set allrecords;
array procedure (3) $ prcode1-prcode3;
do x=(1-3);
if procedure(x) = ('9351'OR '9359') then procedure (x)=1;
end;
run;

 

I keep getting the error statement:

 

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
336:4 336:20 336:29
NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).
336:42
ERROR: Array subscript out of range at line 336 column 4.
ikn=1 prcode1=1257 prcode2=1165 prcode3=1165 rescode=angmar cats=amino x=-2 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.ALLRECORDS.
WARNING: The data set WORK.CUTHIP may be incomplete. When this step was stopped there were 0 observations
and 7 variables.
WARNING: Data set WORK.CUTHIP was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
 


Accepted Solutions
Solution
‎03-07-2018 09:48 PM
Super User
Posts: 6,934

Re: simple array question

As a new user, your errors are understandable.  Here are a few issues with your code.

 

do x=(1-3);

 

1-3 is -2.  You could just as easily have coded:

 

do x=-2;

 

To get x to go from 1 to 3:

 

do x=1 to 3;

 

Next, to compare to a list of values:

 

if procedure(x) in ('9351' '9359') then ...............

 

Finally, you said you wanted to create a new variable.  But instead, your code re-assigns a value to an existing variable.  This would be an improvement:

 

if procedure(x) in ('9351' '9359') then newvar=1;

View solution in original post


All Replies
Super Contributor
Posts: 332

Re: simple array question

Try assigning a character constant to procedure[x] in:

 

if procedure(x) = ('9351'OR '9359') then procedure (x)=1;

 

with '1'.

 

 

Solution
‎03-07-2018 09:48 PM
Super User
Posts: 6,934

Re: simple array question

As a new user, your errors are understandable.  Here are a few issues with your code.

 

do x=(1-3);

 

1-3 is -2.  You could just as easily have coded:

 

do x=-2;

 

To get x to go from 1 to 3:

 

do x=1 to 3;

 

Next, to compare to a list of values:

 

if procedure(x) in ('9351' '9359') then ...............

 

Finally, you said you wanted to create a new variable.  But instead, your code re-assigns a value to an existing variable.  This would be an improvement:

 

if procedure(x) in ('9351' '9359') then newvar=1;

Occasional Contributor
Posts: 13

Re: simple array question

Posted in reply to Astounding

Thanks - little tips like that really help me out in understanding SAS language.

 

What if I wanted to keep only those records/observations (the whole row of data) with '9359' or '9351'? Would I use a keep statement?

Super User
Posts: 6,934

Re: simple array question

To keep or remove observations (based on a calculated variable) use an IF statement.  IF without THEN is totally different than IF-THEN.  To subset the observations, you could add this as the final statement in the DATA step:

 

if newvar=1;

 

Often an IF statement appears earlier in the DATA step.  It is usually most efficient to place it early.  But here you need to calculate NEWVAR first.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 132 views
  • 0 likes
  • 3 in conversation