BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Niugg2010
Obsidian | Level 7

When I run below code. I found only the first record of a column is populated by 'A' and other records are keeping blank for a column. Does someone help me to understand the reason? Thanks

 

data a;
a=' ';
b=1;
do i=1 to 10;
output;
end;
run;
data b;
a='A';
b=1;
run;
proc sort data=a; by b;run;
proc sort data=b; by b; run;
data c;
merge a b;
by b;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS copies the values from a dataset only once.  So for the first observation it reads variables A and B from dataset WORK.A and then overwrites the values read with the values read from WORK.B.  But on the other observation there are no more records to read from WORK.B so the values read from WORK.A are not overwritten. 

 

If you want to only keep the value of A from WORK.B then drop the variable from WORK.A.  You could do it in the step with the MERGE by using the DROP= dataset option.

data c;
  merge a(drop=a) b;
  by b;
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

SAS copies the values from a dataset only once.  So for the first observation it reads variables A and B from dataset WORK.A and then overwrites the values read with the values read from WORK.B.  But on the other observation there are no more records to read from WORK.B so the values read from WORK.A are not overwritten. 

 

If you want to only keep the value of A from WORK.B then drop the variable from WORK.A.  You could do it in the step with the MERGE by using the DROP= dataset option.

data c;
  merge a(drop=a) b;
  by b;
run;
PhilC
Rhodochrosite | Level 12

What did you expect the output to be?  Perhaps we can help you better if we see how you might be thinking.  We can focus on what part of your hypothesis is incorrect.

Niugg2010
Obsidian | Level 7
Thanks a lot. I thought sas read datasets into Program Data Vector(PDV). each time dataset b will overwrite dataset a. so a column should be all populated finally.

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
  • 3 replies
  • 628 views
  • 0 likes
  • 3 in conversation