BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
davkav9
Calcite | Level 5

I'm sure there's a simple solution to this problem, but I'm a new enough user so here goes. I'm using SAS enterprise guide. I have a dataset like so

 

var1 date

1.1  Feb2018

1.4  Mar2018

2.6  Apr2018

3.3  May2018

4.5  Jun2018

 

and I would like to create a column that repeats the earliest var1 value, like so

 

var1 date    base

1.1  Feb2018 1.1

1.4  Mar2018 1.1

2.6  Apr2018 1.1

3.3  May2018 1.1

4.5  Jun2018 1.1

 

Not sure if I should use proc sql or a data step here. Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PrestickNinja
Fluorite | Level 6

Assuming the data is sorted by var, you should be able to use a retain statement?

 

For example:

 

 

data temp;
	set vardate;
	retain base;
if _N_ = 1 then base = var; run;

 

 

You can expand this to grouped data (for example if you had another column ahead) using "BY" and "first." statement.

View solution in original post

3 REPLIES 3
PrestickNinja
Fluorite | Level 6

Assuming the data is sorted by var, you should be able to use a retain statement?

 

For example:

 

 

data temp;
	set vardate;
	retain base;
if _N_ = 1 then base = var; run;

 

 

You can expand this to grouped data (for example if you had another column ahead) using "BY" and "first." statement.

davkav9
Calcite | Level 5
Perfect, exactly what I wanted. Thank you!
novinosrin
Tourmaline | Level 20

  
data have;
input var1 date : anydtdte7.;
format date monyy7.;
cards;
1.1  Feb2018
1.4  Mar2018
2.6  Apr2018
3.3  May2018
4.5  Jun2018
;


proc sql;
create table want as
select *,(select var1  from have  having date=min(date)) as base
from have;
quit;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1450 views
  • 2 likes
  • 3 in conversation