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

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
 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
KachiM
Rhodochrosite | Level 12

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

 

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

 

with '1'.

 

 

Astounding
PROC Star

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;

Angmar
Obsidian | Level 7

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?

Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 4 replies
  • 772 views
  • 0 likes
  • 3 in conversation