BookmarkSubscribeRSS Feed
MattLin
Calcite | Level 5

Have:

v1

some number

. (missing from 2nd row on)

.

Desire:

v1

some number

some number (from 2nd row on)

some number

Thanks.

4 REPLIES 4
ballardw
Super User

Is this a value that only exists on the first record? If so

data want;

     set have;

     retain tempv . ;

     if _n_ = 1 then tempv=v1;

     else v1=tempv;

     drop tempv;

run;

data_null__
Jade | Level 19
data have;
   id = 1;
  
do j=1,2;
     
output;
     
end;
   j=
.;
  
do _n_ = 1 to 5;
     
output;
     
end;
  
stop;
  
run;
data want;
   update have(obs=0) have;
   by id;
   output;
  
run;
Fugue
Quartz | Level 8

This example is just for fun . . . my preference would be the data step methods suggested by either ballardw or DN . . .

/* create some fake data with a variable (v1) that only has a value for the first observation */

data have ;
drop i;
do i = 1 to 100;
  id + 1 ;
  if i=1 then v1 = 100*ranuni(i); /* only create a number for the first record */
  else v1 = .;
  output;
end;
run;

/* "retain" the value using proc sql by doing a dummy merge */

proc sql;
create table want as
select q1.id, q2.v1
from
( select 1 as dummy1, id from have ) q1
, ( select 1 as dummy2, v1 from have (obs=1) ) q2
where q1.dummy1 = q2.dummy2
order by q1.id
;
quit ;

Haikuo
Onyx | Level 15

The more, the merrier:

data have;

  do i=1 to 10;

    if i=1 then var=100;

  else var=.;

  output;

  end;

  drop i;

run;

/*set1--join*/

data want_set1;

set have;

if _n_=1 then set have(obs=1 rename=var=_var);

  var=coalesce(var,_var);

  drop _var;

run;  

/*set2--point= option*/

data want_set2;

    set have;

  if _n_=1 then i=_n_;

  set have point=i;

  run;

  /*set3--DOW*/

  data want_set3;

  do _n_=1 to nobs;

   set have nobs=nobs;

   if _n_=1 then _var=var;

   else var=_var;

   output;

  end;

  drop _var;

  run;

Haikuo

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
  • 1503 views
  • 0 likes
  • 5 in conversation