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

I use firstobs to create lag and merge it with existing data, but every time I have to rename all the columns as C1=_C1, C2=_C2 so that I can drop them easily since they are of no use to me.This is easy if the table has only 4-5 columns but I am working on datasets which have more than 50 columns.So I have to rename them all manually since I don't know how to use macros.

 

Is there a way through which I can create lag for a specific column of a table?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You are NOT(!!!) creating a lag, you create a look-ahead.

 

In the look-ahead table, use a keep= dataset option to keep only the column(s) needed:

data want;
merge
  have
  have (keep=c rename=(c=_c))
;
/* further processing */
run;

View solution in original post

7 REPLIES 7
Ksharp
Super User
IF your variables like C1 C2 .C10 .
could try :

data want;
merge have have( rename=(C1-C10 = _C1-_C10) );
run;
Saurabh_Rana
Obsidian | Level 7
No that was just a example I used to explain the question.
andreas_lds
Jade | Level 19

@Saurabh_Rana wrote:
No that was just a example I used to explain the question.

Then please post an excerpt of the data you have as usable data step, so that we don't have to guess what you have. And please show what you expect as result.

Saurabh_Rana
Obsidian | Level 7
I can't mention the columns but what I am saying is that all the column names are completely different.
Saurabh_Rana
Obsidian | Level 7
Also is there a way to create lead instead of lag using firstobs or some other function in SAS.
Ksharp
Super User

Here is . If you don't mind.

 

data have;
 set sashelp.class;
run;


proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql noprint;
select cats(_name_,'=_',_name_) into :rename separated by ' ' from temp;
quit;
data want;
 merge have have(firstobs=2 rename=(&rename));
run;
Kurt_Bremser
Super User

You are NOT(!!!) creating a lag, you create a look-ahead.

 

In the look-ahead table, use a keep= dataset option to keep only the column(s) needed:

data want;
merge
  have
  have (keep=c rename=(c=_c))
;
/* further processing */
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 609 views
  • 2 likes
  • 4 in conversation