BookmarkSubscribeRSS Feed
Mahip
Obsidian | Level 7

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!

6 REPLIES 6
Reeza
Super User
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.

Astounding
PROC Star

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;

Haikuo
Onyx | Level 15

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;
Peter_C
Rhodochrosite | Level 12
Reads like P. Dorfman code 🙂
Mahip
Obsidian | Level 7

Thank you everyone for your comments! They were very helpful.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 2619 views
  • 4 likes
  • 5 in conversation