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):(Column).
336:4 336:20 336:29
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
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
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;
Try assigning a character constant to procedure[x] in:
if procedure(x) = ('9351'OR '9359') then procedure (x)=1;
with '1'.
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;
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?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.