Hello! So I have a dataset with almost two hundred schools and each school has hundreds of students. I'm trying to add 2 variables to the dataset tied to each school/schoolid. I tried to merge the data (see below) by schoolid, but that only outputed as many results as there are schools. It also dropped all the variables in the original dataset, except for schoolid. How can I add the 2 new variables and have them repeated for each student at the corresponding school? I have an example of the code I ran below:
data newvariables; input schoolid var1 var2$; cards; 1 60 y 2 53 N 3 80 Y 4 20 N ; RUN; DATA mergeddata; merge olddataset newvariables; by schoolid; run;
@statsncats wrote:
Hello! So I have a dataset with almost two hundred schools and each school has hundreds of students. I'm trying to add 2 variables to the dataset tied to each school/schoolid. I tried to merge the data (see below) by schoolid, but that only outputed as many results as there are schools. It also dropped all the variables in the original dataset, except for schoolid. How can I add the 2 new variables and have them repeated for each student at the corresponding school? I have an example of the code I ran below:
data newvariables; input schoolid var1 var2$; cards; 1 60 y 2 53 N 3 80 Y 4 20 N ; RUN; DATA mergeddata; merge olddataset newvariables; by schoolid; run;
Show an example of your "olddataset" with matching schoolid values.
One strongly suspects that your variables that disappeared, if they were named other than the "var1" and "var2" from newvariables, did so prior to the merge.
This example uses your newvariables and creates an olddataset that has has five records for each school with each of the extra 5 variables getting added. So there are a mix of values for the variables in Olddataset and includes a school not in the newvariables. Then merges the result.
No variable disappear. No records disappear. Since there is not a match in newvariables for schoolid=5 those variables have missing values.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.