I have a 12-characters variable sales in the form of, say, "YYYNNNYNYYYN". I am using array to create 12 new variables with one character each. When I use array with substring, I am getting not only 12 variables but also 12 new rows for each observation. So, if I had 50 observations, I am now getting 12*50=600 observations, along with the 12 newly created variables. I am not able to figure out why this is happening.
The syntax I am using is:
array x[12] month1-month12;
do i=1 to 12;
x[i]=substr(sales, i, 1);
output;
end;
drop i;
run;
Thank you!
array x[12] month1-month12;
do i=1 to 12;
x[i]=substr(sales, i, 1);
output;
end;
drop i;
run;
OUTPUT is within the loop. Move it to outside of the loop, so it outputs at the END of the process. Now it outputs every record*month so you get N*12 records in total. FYI- you can use the CHAR or Substr() to loop this, I find the CHAR function more useful.
Another option - if you read this file from a CSV or other dataset you may be able to modify the input to read all the records initially.
Get rid of the OUTPUT statement to get rid of the extra observations.
Also note, you may be creating your new variables as numeric instead of character. If the new variables are not already defined earlier in your program, change the array statement:
array x[12] $ 1 month1-month12;
Ok, this is just for fun :),
data test;
sales='YYYNNNYNYYYN';
array x[12] $ 1 month1-month12;
call pokelong(sales,addrlong(x(1)),dim(x));
run;
Indeed!
Thank you everyone for your comments! They were very helpful.
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.