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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 514 views
  • 0 likes
  • 5 in conversation