BookmarkSubscribeRSS Feed
Saleem
Calcite | Level 5

Data ds;
Cards;
1
2
3
4
5
6
;
Run;
How to add new observation in 3.5 in after 3

4 REPLIES 4
smantha
Lapis Lazuli | Level 10
Method 1:
Data ds;
input x;
Cards;
1
2
3
3.5
4
5
6
;
run;

Method 2:
Data ds;
input x;
Cards;
1
2
3
4
5
6
;

data ds;
done=0;
do until(done);
 set ds end=done;
output;
end;
x=3.5; output;
drop done;
stop;
run;

proc sort; by x;
run;
Saleem
Calcite | Level 5

Output like that
1
2
3
3.5
4
5
6
Like that

Kurt_Bremser
Super User

Please test your code before posting it, and use a descriptive subject line. I edited your subject line


@Saleem wrote:
Data ds;
Cards;
1
2
3
4
5
6
;
Run;
How to add new observation in 3.5 in after 3


.

ballardw
Super User

@Saleem wrote:
Data ds;
Cards;
1
2
3
4
5
6
;
Run;
How to add new observation in 3.5 in after 3

You should provide a generic rule. Is this going to happen every time the value 3 appears or just once? Is the value supposed to be the mean of the current and the following value? Are any other variables going to be involved? If so, where do their values come from (the record with the 3 or somewhere else).

 

One way is to have a data set with just the value of 3.5, append the sets and sort.

Your data step does not run as expected because there is no INPUT and so no variable is created. I take the liberty of providing a variable.

Or

Data ds;
   input value;
Cards;
1
2
3
4
5
6
;
Run;

data want; 
   set ds;
   output;
   if value=3 then do;
      value=3.5;
      output;
   end;
run;

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
  • 4 replies
  • 1251 views
  • 2 likes
  • 4 in conversation