If I have a data set A:
ID Ratio
1 5
. 5
10 5
Data set B:
ID Time conc
1 0 0
. 1 0.5
24 24 0.01
The (.) means that there are several values for A that end at 10 while for B they end at 24. The question is how can I introduce the constant ratio value 5 into data B from data A for all the rows in data set B?
Post actual data. Like this:
data a;
do id=1 to 10;
ratio=2.5;
output;
end;
run;
data b;
id=0;
do time=1 to 24 ;
id=id+1;
conc = round(ranuni(0),0.01);
output;
end;
run;
Now use that data:
data want;
set b;
if _n_=1 then set a(keep=ratio);
run;
Result:
Obs id time conc ratio 1 1 1 0.31 2.5 2 2 2 0.74 2.5 3 3 3 0.52 2.5 4 4 4 0.84 2.5 5 5 5 0.09 2.5 6 6 6 0.33 2.5 7 7 7 0.11 2.5 8 8 8 0.87 2.5 9 9 9 0.61 2.5 10 10 10 0.43 2.5 11 11 11 0.92 2.5 12 12 12 0.79 2.5 13 13 13 0.91 2.5 14 14 14 0.98 2.5 15 15 15 0.41 2.5 16 16 16 0.50 2.5 17 17 17 0.21 2.5 18 18 18 0.55 2.5 19 19 19 0.31 2.5 20 20 20 0.38 2.5 21 21 21 0.43 2.5 22 22 22 0.49 2.5 23 23 23 0.71 2.5 24 24 24 0.96 2.5
The only way that running that step would result in any missing values for RATIO would be if the variable already existed in dataset B. In that case you could just drop it.
data want;
set b(drop=ratio);
if _n_=1 then set a(keep=ratio);
run;
It is still not clear why you made the A dataset have 10 observations instead of one.
I don't follow. You want a constant?
data want;
set B;
ratio=5;
run;
You want the constant that is in dataset A? Just take the first copy of it.
data want;
set B;
if _n_=1 then set A(keep=ratio);
run;
If you want something else you need to explain the rules in more detail.
Post actual data. Like this:
data a;
do id=1 to 10;
ratio=2.5;
output;
end;
run;
data b;
id=0;
do time=1 to 24 ;
id=id+1;
conc = round(ranuni(0),0.01);
output;
end;
run;
Now use that data:
data want;
set b;
if _n_=1 then set a(keep=ratio);
run;
Result:
Obs id time conc ratio 1 1 1 0.31 2.5 2 2 2 0.74 2.5 3 3 3 0.52 2.5 4 4 4 0.84 2.5 5 5 5 0.09 2.5 6 6 6 0.33 2.5 7 7 7 0.11 2.5 8 8 8 0.87 2.5 9 9 9 0.61 2.5 10 10 10 0.43 2.5 11 11 11 0.92 2.5 12 12 12 0.79 2.5 13 13 13 0.91 2.5 14 14 14 0.98 2.5 15 15 15 0.41 2.5 16 16 16 0.50 2.5 17 17 17 0.21 2.5 18 18 18 0.55 2.5 19 19 19 0.31 2.5 20 20 20 0.38 2.5 21 21 21 0.43 2.5 22 22 22 0.49 2.5 23 23 23 0.71 2.5 24 24 24 0.96 2.5
The only way that running that step would result in any missing values for RATIO would be if the variable already existed in dataset B. In that case you could just drop it.
data want;
set b(drop=ratio);
if _n_=1 then set a(keep=ratio);
run;
It is still not clear why you made the A dataset have 10 observations instead of one.
SET (and MERGE) are executable statements. Each time it execute it reads another observation from the dataset(s).
That is why you can do code like this to process all of the records for a group in iteration of the data step.
do until(last.id);
set have;
by id;
....
end;
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!
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.