How to repeat the same constant through a column

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

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.

View solution in original post


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.

Trusted Advisor
Posts: 1,204

Re: How to repeat the same constant through a column

Hi Matt,

How about this one?

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;

Respected Advisor
Posts: 4,644

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 181 views
  • 6 likes
  • 4 in conversation