I define a 7-element array and put a value in one or more of the elements but never two of them. (Why you say. Because LEC16 (see code) by design never includes a value for i1=2 or 5.) I later drop those two elements. But, i get a warning that those two elements were never addressed and the elements are not dropped. I understand the warning as useful but why weren't the elements dropped? Here's the code for one of the arrays. The drop command that follows is just drop LEC16_2 LEC16_5;
* LEC_16; array vlec16{7} A3LEC16_1-A3LEC16_7; nlen=lengthn(LEC_16); if (nlen ge 1) then do; nlen=1+(nlen-1)/2; do i=1 to nlen; i1=1+2*(i-1); val=substr(LEC_16,i1,1); vlec16{input(val,BEST32.)}=1; end; end;
You really should show the entire data step. You do not show a DROP statement or option anywhere, so can't address why not.
You don't actually show where Lec16_2 or Lec16_5 might be used. You do not create them, they are not shown in your array statement. Your Array has
A3LEC16_1-A3LEC16_7
Not Lec16_2 or Lec16_5. So you did you intent to reference A3Lec16_2????
@emaguin wrote:
I could have said array vlec16{5} A3LEC15_1 A3LEC15_3 A3LEC15_4 A3LEC15_6 A3LEC15_7; but why?? It's simpler to do what I did and then not use two of the variables and then delete them. So what! Like I said, understand the warning. But why did sas not execute the statement? It was a warning not an error.
Gene Maguin
Because you did not "drop" A3Lec15_2 (or A3Lec16_2) you said the problem was with Lec16_2. You are now using different variable names than shown in your previous code snippet (A3Lec15). Which is one reason I asked for the entire data step, to see what you may be creating during the data step, exactly what you are "dropping". I strongly suspect you didn't create the variables that you claimed to attempt to drop.
A couple of other ways to parse comma delimited string of values and get a 1 or 1/0 coded variable.
data example; infile datalines truncover; input string $15.; array v(7); do i=1 to countw(string,','); v[input(scan(string,i,','),f8.)]=1; end; datalines; 1 1,3 3,4,7 1,3,7 1,3,4,6,7 ; /* or to do 1/0 coding*/ data example; infile datalines truncover; input string $15.; array v(7); do i=1 to 7; v[i]= index(string,put(i,f1.))>0; end; datalines; 1 1,3 3,4,7 1,3,7 1,3,4,6,7 ;
Scan can pull a specific position number of a value from a delimited string. The last parameter is a list of delimiters. Lots simpler than trying to use the length and an offset. Probably not needed but sometimes you want to break up a string at different points and though it appropriate to include a specific delimiter in this example. The Index function returns a position in a string of a substring. SAS returns 1 for true or 0 for false with comparing the returned value from Index to > 0.
Index would not be appropriate for finding 1 if you might have values like 10, 11 or 101 though INDEXW or FINDW might.
I prefer 1/0 coded values for "any that apply" type questions because then a sum=total number of selections, mean=percentage of respondents that selected the choice.
BTW, 190 lines isn't particularly long for a data step.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.