## How to repeat the same constant through a column

Solved
Occasional Contributor
Posts: 14

# How to repeat the same constant through a column

how to repeat the value of the first row through the whole column?

What I have:

v1 v2  target_v (only the first row has the desired value)

1   2     3.14

3   4       .

.

And I want to turn the target_v into 3.14 for the whole column.

Thank you!!

- Matt

Accepted Solutions
Solution
‎08-07-2013 02:43 PM
Super Contributor
Posts: 543

## Re: How to repeat the same constant through a column

Hi.

You can use the retain:

data in;

input v1 v2  target_v ;

cards;

1   2     3.14

3   4       .

;

data want;

set in;

retain temp;

if target_v ne . then temp  = target_v;

target_v = temp;

drop temp;

run;

Good luck!

Anca.

All Replies
Solution
‎08-07-2013 02:43 PM
Super Contributor
Posts: 543

## Re: How to repeat the same constant through a column

Hi.

You can use the retain:

data in;

input v1 v2  target_v ;

cards;

1   2     3.14

3   4       .

;

data want;

set in;

retain temp;

if target_v ne . then temp  = target_v;

target_v = temp;

drop temp;

run;

Good luck!

Anca.

Posts: 1,270

## Re: How to repeat the same constant through a column

Hi Matt,

data have;

Input v1 v2  target_v;

datalines;

1   2     3.14

3   4       .

5 6  .

;

data target (keep=target_v);

set have (obs=1);

run;

DATA want;

SET have (drop=target_v);

IF _N_ = 1 THEN SET target;

RUN;

proc print data=want;

run;

Posts: 5,539

## Re: How to repeat the same constant through a column

The simpler, the better :

data have;
Input v1 v2  target_v;
datalines;
1   2     3.14
3   4       .
5 6  .
;

data want;
if missing(target_v) then set have;
set have (drop=target_v);
run;

PG

PG
Super Contributor
Posts: 543

## Re: How to repeat the same constant through a column

Nifty!:smileyblush:

🔒 This topic is solved and locked.