I am attempting to run the code below in an attempt to recreate variables n1-n5 from the array. The problem is, it is not currenlty working and I can't figure out why.
Data STRING;
Input STRING $10.
X 1-2
Y 3
N1 6
N2 7
N3 8
N4 9
N5 10;
State= Upcase (substr(STRING, 4,2));
Array N[5] $5 N1-N5;
DO I= 1 to 5;
N [I]= scan(String, I,.);
end;
Drop I;
Datalines;
123nj76543
892NY10203
876pA83745
;
@FrustratedBio wrote:
Hello again and thank you for your reply. I am trying to use the scan function in an array to generate values N1-N5---same as the ones in the input statement, only the question requires an array. I just put them in the input for reference. There is no error message, but the array is not outputting any values from the string.
What is the delimiter that you want to use for the SCAN() function? I do not see any delimiter in your source text (datalines/cards).
If you don't have a delimiter than SCAN() is not the function to use.
If you just want a single character try the CHAR() function.
@FrustratedBio wrote:
I am attempting to run the code below in an attempt to recreate variables n1-n5 from the array. The problem is, it is not currenlty working and I can't figure out why.
What does the highlighted part mean? Do you get any errors or is the result not meeting your expectations? So please explain what you expect. If there are any unexpected notes in the log, post it using the "insert code"-button.
@FrustratedBio wrote:
Hello again and thank you for your reply. I am trying to use the scan function in an array to generate values N1-N5---same as the ones in the input statement, only the question requires an array. I just put them in the input for reference. There is no error message, but the array is not outputting any values from the string.
What is the delimiter that you want to use for the SCAN() function? I do not see any delimiter in your source text (datalines/cards).
If you don't have a delimiter than SCAN() is not the function to use.
If you just want a single character try the CHAR() function.
@FrustratedBio wrote:
I am attempting to run the code below in an attempt to recreate variables n1-n5 from the array. The problem is, it is not currenlty working and I can't figure out why.
Data STRING;
Input STRING $10.
X 1-2
Y 3
N1 6
N2 7
N3 8
N4 9
N5 10;
State= Upcase (substr(STRING, 4,2));
Array N[5] $5 N1-N5;
DO I= 1 to 5;
N [I]= scan(String, I,.);
end;
Drop I;
Datalines;
123nj76543
892NY10203
876pA83745
;
One of the main problems you are having is that your read values into variables N1 through N5 as numeric values and then try to force use as character values, the $5 in the Array and Scan function.
Once a variable is created, and the input statement is implicitly creating everything except String as numeric, the type cannot change.
Is this what you expected:
Data STRING; Input STRING $10. X 1-2 Y 3 N1 6 N2 7 N3 8 N4 9 N5 10; State= Upcase (substr(STRING, 4,2)); Datalines; 123nj76543 892NY10203 876pA83745 ;
or maybe
Data STRING; Input STRING $10. X 1-2 Y 3 N1 $ 6 N2 $ 7 N3 $ 8 N4 $ 9 N5 $ 10; State= Upcase (substr(STRING, 4,2)); Datalines; 123nj76543 892NY10203 876pA83745 ;
or maybe
Data STRING; Input STRING $10. X 1-2 Y 3 N1 6 N2 7 N3 8 N4 9 N5 10; State= Upcase (substr(STRING, 4,2)); Array Z[5] $5 Z1-Z5; DO I= 1 to 5; Z[I]= substr(String,i+5,1); end; Drop I; Datalines; 123nj76543 892NY10203 876pA83745 ;
Read you logs. Your first code shows these messages
258 Data STRING; 259 Input STRING $10. 260 X 1-2 261 Y 3 262 N1 6 263 N2 7 264 N3 8 265 N4 9 266 N5 10; 267 State= Upcase (substr(STRING, 4,2)); 268 Array N[5] $5 N1-N5; 269 DO I= 1 to 5; 270 N [I]= scan(String, I,.); 271 end; 272 Drop I; 273 Datalines; NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 270:23 NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 270:1 NOTE: Invalid numeric data, '123nj76543' , at line 270 column 8. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+-- 274 123nj76543 STRING=123nj76543 X=12 Y=3 N1=. N2=. N3=. N4=. N5=. State=NJ I=6 _ERROR_=1 _N_=1 NOTE: Invalid numeric data, '892NY10203' , at line 270 column 8. 275 892NY10203 STRING=892NY10203 X=89 Y=2 N1=. N2=. N3=. N4=. N5=. State=NY I=6 _ERROR_=1 _N_=2 NOTE: Invalid numeric data, '876pA83745' , at line 270 column 8. 276 876pA83745 STRING=876pA83745 X=87 Y=6 N1=. N2=. N3=. N4=. N5=. State=PA I=6 _ERROR_=1 _N_=3 NOTE: The data set WORK.STRING has 3 observations and 9 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
The invalid data on line 270 is from the SCAN function. The dot in your scan is not a valid SCAN function third parameter. It has to hold a character value either as a variable or a quoted string such as ".". But there is no obvious delimiter or separator in the value of String so you need a different approach to get values out of it with character functions.
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 16. 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.