BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
lapetitemaman
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

 

View solution in original post

7 REPLIES 7
ballardw
Super User

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.

 

 

lapetitemaman
Calcite | Level 5
Thank you so much for your help. I did not know that I can paste code into a code box here. I will do that in the future.

For this sample run, I only set 50 replications (but I only used 3 in the data example here). I have tried your code but all the 'replicate' variable is 1 instead of 1, 1, 1, 2, 2, 2, 3, 3, 3. Is there something that I have missed?

Thanks again.
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
lapetitemaman
Calcite | Level 5
This data set shows results from 3 replications. Each replication contains 3 observations. I am testing my code before I increase the number of replications but haven't been successful.
PaigeMiller
Diamond | Level 26

If it is always 3 replications, then the code provided by others is fine.

--
Paige Miller
Reeza
Super User

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.

Reeza
Super User
data need;
set have;
retain replication 0;

if entryNumber=1 then replication+1;

run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 794 views
  • 0 likes
  • 4 in conversation