DATA Step, Macro, Functions and more

rename and label

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

rename and label

question 1.

rename oldvar=newvar;label oldvar='label';  ----> why we should give label to oldvar even though we already renamed in datastep?

But in the proc dataset

rename oldvar=newvar;label newvar='label';  ----> why we should give label to newvar(reverse to datastep)?

question2.  the following statements should be completed one after the other. need in datastep only and if possible,without point concept

data oneset twosets onemerge twomerge;

set c d; output oneset;

set c;set d;output twosets;

merge c d;output onemerge;

merge c;merge d;output twomerge;

run;


Accepted Solutions
Solution
‎09-16-2014 02:27 PM
Super User
Super User
Posts: 6,502

Re: rename and label

1)  Does because that is how it works help?

Actually you can label the old variable name in PROC DATASETS, just do it BEFORE you rename it.  PROC DATASETS is a tool for modifying data sets. So you can make multiple modifications at once. In a data step the whole step is the definition of what the output will be.  You cannot change the name that you use to refer to a variable while the step is running.  The RENAME statement in the data step just changes the name that the variable will have in the output data set. That is it changes how the NEXT step should reference the variable.

2) I have no idea what you are asking. The first three might make some sense but the last does not. But if you want to write out records in different order in the same step then you will need to do something that lets you manipulate the (such as the POINT= option on the SET statement).  It is probably easier to not do this at all.  Or if you must then it will require more than one data step.

This makes sense . You are just concatenating the two tables.

data oneset ;

set c d;

run;

What is the second one? Do you want to interleave the records from C and D?  That only makes sense if the two tables are exactly the same length, otherwise the longer one will be truncated  when the data step stops because it reached the end of the shorter file.  Or you could use the EOF= option and test that you haven't read past the end of the shorter table.  You will also need to check when to stop the data step.

data twoset;

set c;

output;

set d;

output;

run;

Third one looks like a merge. This would essentially attach the D dataset variables to the right of the C dataset variables. But you should have a BY statement.

data onemerge;

merge c d;

BY IDVAR ;

run;


Fourth one I am not sure what you want.  Are you trying to make a checker board pattern where odd observations have values for the C varaibles and missing values for the D variables and the even ones the reverse?


View solution in original post


All Replies
Solution
‎09-16-2014 02:27 PM
Super User
Super User
Posts: 6,502

Re: rename and label

1)  Does because that is how it works help?

Actually you can label the old variable name in PROC DATASETS, just do it BEFORE you rename it.  PROC DATASETS is a tool for modifying data sets. So you can make multiple modifications at once. In a data step the whole step is the definition of what the output will be.  You cannot change the name that you use to refer to a variable while the step is running.  The RENAME statement in the data step just changes the name that the variable will have in the output data set. That is it changes how the NEXT step should reference the variable.

2) I have no idea what you are asking. The first three might make some sense but the last does not. But if you want to write out records in different order in the same step then you will need to do something that lets you manipulate the (such as the POINT= option on the SET statement).  It is probably easier to not do this at all.  Or if you must then it will require more than one data step.

This makes sense . You are just concatenating the two tables.

data oneset ;

set c d;

run;

What is the second one? Do you want to interleave the records from C and D?  That only makes sense if the two tables are exactly the same length, otherwise the longer one will be truncated  when the data step stops because it reached the end of the shorter file.  Or you could use the EOF= option and test that you haven't read past the end of the shorter table.  You will also need to check when to stop the data step.

data twoset;

set c;

output;

set d;

output;

run;

Third one looks like a merge. This would essentially attach the D dataset variables to the right of the C dataset variables. But you should have a BY statement.

data onemerge;

merge c d;

BY IDVAR ;

run;


Fourth one I am not sure what you want.  Are you trying to make a checker board pattern where odd observations have values for the C varaibles and missing values for the D variables and the even ones the reverse?


Contributor
Posts: 44

Re: rename and label

thanks for the reply.

I understood. the interviewer asked me this question

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 2346 views
  • 0 likes
  • 2 in conversation