Variable values

Reply
Contributor
Posts: 60

Variable values

hi,

I have this kind of variable (from 3 observation only the first one has value):

DATE

12 Nov 2013

13 Nov 2013

14 Nov 2013

and I want to make it like this:

DATE

12 Nov 2013

12 Nov 2013

12 Nov 2013

13 Nov 2013

13 Nov 2013

13 Nov 2013

14 Nov 2013

14 Nov 2013

14 Nov 2013



Esteemed Advisor
Esteemed Advisor
Posts: 7,223

Re: Variable values

Hi,

data have;

  a=1; date="12 Nov 2013"; output;

  a=2; date=""; output;

  a=3; date=""; output;

  a=1; date="13 nov 2013"; output;

run;

data want (drop=old_date);

  set have (rename=(date=old_date));

  retain date;

  if old_date ne "" then date=old_date;

run;

Valued Guide
Posts: 854

Re: Variable values

Do you have an id number of some kind?  If so I'd make sure it was sorted like this, that'll make sure all of your dates are in the right place.:

data have;

infile cards dsd;

informat date date9.;

format date date9.;

input ID$ date;

cards;

1,12NOV2013

1,

1,

2,13NOV2013

2,

2,

3,14NOV2013

3,

3,

;

proc sort data=have;by id descending date;

data want;

set have;

by id descending date;

retain _date;

if first.id then _date = date;

if missing(date) then date = _date;

drop _:;

run;

Contributor
Posts: 60

Re: Variable values

I gave just very small part of my dataset, in overall i have hundreds of date observations (not just three) and i have more variables.

Esteemed Advisor
Esteemed Advisor
Posts: 7,223

Re: Variable values

I doesn't matter how many observations there are, the logic is the same:

- Step 1 - Sort the data into the order you require it.

- Step 2 - Loop over the dataset (datastep), and for each row identify if variable is missing and if so replace the missing with the retained value.

Respected Advisor
Posts: 4,981

Re: Variable values

Here's an approach that is quick and easy.  However, note that it will affect missing values for ALL your variables, not just ones that you have in mind such as DATE:

data middle;

set have;

dummy='abc';

run;

data want (drop=dummy);

  update middle (obs=0) middle;

  by dummy;

  output;

run;

Respected Advisor
Posts: 3,775

Re: Variable values

Astounding wrote:

Here's an approach that is quick and easy.  However, note that it will affect missing values for ALL your variables, not just ones that you have in mind such as DATE:

When you have variable that you want want to LOCF then bring them in using a SET statement.

data have;
   input DATE1 $16.;
   date2 = date1;
  
if not missing(date1) then set sashelp.class point=_n_;
   output;
  
call missing(of _all_);
   cards;
12 Nov 2013


13 Nov 2013


14 Nov 2013
;;;;
   run;
proc print;
  
run;
data middle / view=middle;
   set have;
   dummy='abc';
  
run;
data want;
   update middle(obs=0 keep=dummy) middle(keep=dummy dateSmiley Happy;
   by dummy;
   set have(drop=dateSmiley Happy;
   output;
  
drop dummy;
   run;
proc print;
  
run;

Capture.PNG

Capture.PNG
Occasional Contributor
Posts: 10

Re: Variable values

check the below code ,this will work:

data have;

input DATE;

format date date9.;

informat date date9.;

cards;

12Nov2013

13Nov2013

14Nov2013

;

run;

data want(drop=i);

set have;

do i=0 to 2 ;

output;

end;

run;

Occasional Contributor
Posts: 11

Re: Variable values

data filled (drop=_date);

   set empty (rename=(date=_date));

   retain date;

   if _date NE . then date = _date;

run;

Hth, Eric

Ask a Question
Discussion stats
  • 8 replies
  • 352 views
  • 0 likes
  • 7 in conversation