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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 975 views
  • 0 likes
  • 3 in conversation