task > Create two arrays that can be used to access the number of students in grades 7
through 12 from each of your two input data sets. Create a third array that will
create and access new variables named Grade7 through Grade12.
i have two data sets ok_mid and ok_high, now i want to make two arrays to access Grade7 - Grade12 from both data sets to add them. So far i have only seen one array - one data sets in a scope examples. how can i achieve above task
thanks.
proc sort data=data.ok_mid out=mid_sorted(rename=(Grade7-Grade12=MS_Grade7-MS_Grade12)); by MapCity School; run; proc sort data=data.ok_high out=high_sorted(rename=(Grade7-Grade12=HS_Grade7-HS_Grade12)); by MapCity School; run; data Both Midonly (keep=School MapCity MailCity County) HSonly (keep=School MapCity MailCity County); merge mid_sorted (in=A) high_sorted (in=B); by MapCity School; if A=1 and B=1 then output Both; /*(b) create a temporary data set of high schools with no matching record in ok_mid*/ else if A=0 and B=1 then output HSonly; /*(c) create a temporary data set of middle schools with no matching record in ok_high*/ else if A=1 and B=0 then output Midonly; run; data; set high_sorted; array hs_array {6} HS_Grade7-HS_grade12; set mid_sorted; array ms_array {6} MS_Grade7-MS_Grade12; array new_array {6} Grade7-Grade12; do i =1 to 6; new_array[i] = hs_array[i] + ms_array[i]; end; run;
In your last step you access high_sorted, its likely you want to access the both dataset - i.e. the dataset you merge before hand to get all those variables into one dataset. Maybe something like this (and updated the formatting so its more readable - also note that a and b are bolean 1 or 0, so you don't need the a=1, just a);
proc sort data=data.ok_mid out=mid_sorted (rename=(grade7-grade12=ms_grade7-ms_grade12)); by mapcity school; run; proc sort data=data.ok_high out=high_sorted (rename=(grade7-grade12=hs_grade7-hs_grade12)); by mapcity school; run; data both midonly (keep=school mapcity mailcity county) hsonly (keep=school mapcity mailcity county); merge mid_sorted (in=a) high_sorted (in=b); by mapcity school; if a and b then output both; else if not(a) and b then output hsonly; else output midonly; run; data; set both; array hs_array {6} hs_grade7-hs_grade12; array ms_array {6} ms_grade7-ms_grade12; array new_array {6} grade7-grade12; do i=1 to 6; new_array{i}=hs_array{i} + ms_array{i}; end; run;
In your last step you access high_sorted, its likely you want to access the both dataset - i.e. the dataset you merge before hand to get all those variables into one dataset. Maybe something like this (and updated the formatting so its more readable - also note that a and b are bolean 1 or 0, so you don't need the a=1, just a);
proc sort data=data.ok_mid out=mid_sorted (rename=(grade7-grade12=ms_grade7-ms_grade12)); by mapcity school; run; proc sort data=data.ok_high out=high_sorted (rename=(grade7-grade12=hs_grade7-hs_grade12)); by mapcity school; run; data both midonly (keep=school mapcity mailcity county) hsonly (keep=school mapcity mailcity county); merge mid_sorted (in=a) high_sorted (in=b); by mapcity school; if a and b then output both; else if not(a) and b then output hsonly; else output midonly; run; data; set both; array hs_array {6} hs_grade7-hs_grade12; array ms_array {6} ms_grade7-ms_grade12; array new_array {6} grade7-grade12; do i=1 to 6; new_array{i}=hs_array{i} + ms_array{i}; end; run;
First, get your data in the form you need it. Are the multiple SET statements in the last step creating a dataset in the right format.
If no, figure that out first and then you can declare as many arrays as you want. I suspect you should be using the BOTH dataset. Or a version with all of the data.
Arrays in SAS are only a short cut reference to the variables. They don't store data per se, just make it easier to reference a variable with an index.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.