BookmarkSubscribeRSS Feed
statsncats
Calcite | Level 5

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;

 

1 REPLY 1
ballardw
Super User

@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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 449 views
  • 0 likes
  • 2 in conversation