BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_it
Quartz | Level 8
I want to use two array variables . Example studname, percentage. I want to repeat the loop for 40 students and create the dataset. Pls help.
1 ACCEPTED SOLUTION

Accepted Solutions
nrk1787db1
Obsidian | Level 7
Data A ;
ARRAY studname {40} $ studname1-studname40 ;
ARRAY percentage {40} perc1-perc40 ;
 do i=1 to 40 ;
   StudName[i]= 'A' || put(i,2.) ;
   percentage[i]=1 + i*2 ;
 end ;
run ;

Proc print ;

See if this helps

View solution in original post

9 REPLIES 9
nrk1787db1
Obsidian | Level 7
Data A ;
ARRAY studname {40} $ studname1-studname40 ;
ARRAY percentage {40} perc1-perc40 ;
 do i=1 to 40 ;
   StudName[i]= 'A' || put(i,2.) ;
   percentage[i]=1 + i*2 ;
 end ;
run ;

Proc print ;

See if this helps

sas_it
Quartz | Level 8
Thank you. Can you pls explain the program also.
Sajid01
Meteorite | Level 14

Thanks @nrk1787db1 for the wonderful solution.
I see  that all the array elements (both for student and percentage) come in a single row.
Can we not have one row for each students.
Thus the dataset will have two columns and 40  rows instead of one row and 80 columns.
Thanks once again

Tom
Super User Tom
Super User

Can you clarify what you want?

If you just want to make a dataset with 40 students you don't normally need to use any arrays.

sas_it
Quartz | Level 8
I want to create dataset for 100 students and upLoad on VA for dashboard.
Tom
Super User Tom
Super User

@sas_it wrote:
I want to create dataset for 100 students and upLoad on VA for dashboard.

Normally to create a dataset you just read in the data from some source.  Such as lines of text included in the program.

data students;
  input id age score ;
cards;
1 12 100
2 13 85
3 14 78
;

If you want to make up example data you can use a DO loop with an OUTPUT statement to write multiple observations.

data students;
  do id=1 to 100;
     output;
  end;
run;

Again arrays really do not come into play here.  Arrays are for when you have multiple variables that contain similar data and you want to loop over them (or index into them).

Sajid01
Meteorite | Level 14

Thanks @Tom 
I have the arrays and the specific example of @nrk1787db1 in mind.

Tom
Super User Tom
Super User

@Sajid01 wrote:

Thanks @Tom 
I have the arrays and the specific example of @nrk1787db1 in mind.


Again no arrays needed.

data students;
  length name $8 percent 8;
  do i=1 to 40;
    name = cats('A',put(i,z2.));
    percent=1 + 2*i ;
    output;
  end;
  drop i;
run;

Results:

image.png

sas_it
Quartz | Level 8
Thank you.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 710 views
  • 4 likes
  • 4 in conversation