Greetings everyone,
I have a data set that is generated from replications and I need to add a replication number to each set.
data have;
input entrynumber var1 var2;
cards;
1 -1.189 -2.655
2 -0.735 -1.896
3 0.452 0-0.984
1 -0.291 1.998
2 0.003 -0.015
3 1.267 2.974
1 0.192 -0.523
2 -1.715 -0.569
3 2.876 1.998
;
data need;
input entrynumber var1 var2 replication;
cards;
1 -1.189 -2.655 1
2 -0.735 -1.896 1
3 0.452 0-0.984 1
1 -0.291 1.998 2
2 0.003 -0.015 2
3 1.267 2.974 2
1 0.192 -0.523 3
2 -1.715 -0.569 3
3 2.876 1.998 3
;
How do I achieve this? Do I use do loop?
Any input will be greatly appreciated.
First, make sure that your data step generates the values you intend. This line is malformed to read with that input as 0-0.984 is not a valid number.
3 0.452 0-0.984 1
Second, it is a good idea to paste code into a code box opened on the forum with the </> icon that appears along the top of the message window. The body text of the message windows will get reformatted by the forum software and may insert/remove/replace characters so that an data step will not run correctly.
If you know exactly how many records are in each replicate and the value is in the data as shown:
data want; set have; retain replicate 1; output; if entrynumber=3 then replicate+1; run;
Retain creates variables that maintain values across iterations of the data step and optionally assign a starting value.
The explicit output writes data to the output set before comparing the value of enterynumber to increment the replicate number.
BTW, depending on what/why the data is created if you use Proc Surveyselect to create a replicate sample then it will insert a replicate number.
First, make sure that your data step generates the values you intend. This line is malformed to read with that input as 0-0.984 is not a valid number.
3 0.452 0-0.984 1
Second, it is a good idea to paste code into a code box opened on the forum with the </> icon that appears along the top of the message window. The body text of the message windows will get reformatted by the forum software and may insert/remove/replace characters so that an data step will not run correctly.
If you know exactly how many records are in each replicate and the value is in the data as shown:
data want; set have; retain replicate 1; output; if entrynumber=3 then replicate+1; run;
Retain creates variables that maintain values across iterations of the data step and optionally assign a starting value.
The explicit output writes data to the output set before comparing the value of enterynumber to increment the replicate number.
BTW, depending on what/why the data is created if you use Proc Surveyselect to create a replicate sample then it will insert a replicate number.
What is the rule to determine when you increment the replication number? In this simple example, it could be a couple of things. What is the real world rule to use on your data?
If it is always 3 replications, then the code provided by others is fine.
If the number of reps is variable, use the method of incrementing when it's 1, rather than at the end, demonstrated in my solution.
data need;
set have;
retain replication 0;
if entryNumber=1 then replication+1;
run;
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.