Hello all, I try to rename a variable and then keep only several variables in the dataset, but I meet some problem, can anyone help me with the following code and tell me what went wrong, thank you so much!
data nh.Dietaryday1_4yr;
set nh.dr1iff_c (rename=(DR1DAY=DR1DAY_IFF))
nh.dr1iff_d (rename=(DR1DAY=DR1DAY_IFF));
run;
data nh.Dietaryday1_4yr;
set nh.dr1iff_c (keep =seqn dr1ikcal DR1DAY_IFF )
nh.dr1iff_d (keep=seqn dr1ikcal DR1DAY_IFF);
RUN;
ERROR: The variable DR1DAY_IFF in the DROP, KEEP, or RENAME list has never been referenced.
You code doesn't work because you are not changing the names of the variables in data set nh.dr1iff_c. You are only changing the names of the variables in data set nh.dr1iff_c when it is used to create nh.Dietaryday1_4yr, and the names the next time you use nh.dr1iff_c are not renamed. So in your second data set, the names are the original variable names in nh.dr1iff_c
Try this:
data nh.Dietaryday1_4yr;
set nh.dr1iff_c (rename=(DR1DAY=DR1DAY_IFF) keep=dr1day seqn dr1ikcal)
nh.dr1iff_d (rename=(DR1DAY=DR1DAY_IFF) keep=dr1day seqn dr1ikcal);
run;
I can only guess what you are trying to do, but I think it could be:
data nh.Dietaryday1_4yr;
set
nh.dr1iff_c (keep =seqn dr1ikcal DR1DAY rename=(DR1DAY=DR1DAY_IFF))
nh.dr1iff_d (keep =seqn dr1ikcal DR1DAY rename=(DR1DAY=DR1DAY_IFF));
run;
You code doesn't work because you are not changing the names of the variables in data set nh.dr1iff_c. You are only changing the names of the variables in data set nh.dr1iff_c when it is used to create nh.Dietaryday1_4yr, and the names the next time you use nh.dr1iff_c are not renamed. So in your second data set, the names are the original variable names in nh.dr1iff_c
Try this:
data nh.Dietaryday1_4yr;
set nh.dr1iff_c (rename=(DR1DAY=DR1DAY_IFF) keep=dr1day seqn dr1ikcal)
nh.dr1iff_d (rename=(DR1DAY=DR1DAY_IFF) keep=dr1day seqn dr1ikcal);
run;
That makes sense!
Thank you!
It might be clearer to use the RENAME and KEEP statements instead of the RENAME= and KEEP= dataset options. Especially because you are doing the same rename and keep for both input datasets. Note that this way during the data step DR1DAY is the name to use. The RENAME statement impacts what name is used in the output dataset.
data nh.Dietaryday1_4yr;
set nh.dr1iff_c nh.dr1iff_d;
keep seqn dr1ikcal DR1DAY;
rename DR1DAY=DR1DAY_IFF ;
run;
What is happening if this is the actual code you submit is that you create the nh.Dietaryday1_4yr
data nh.Dietaryday1_4yr; set nh.dr1iff_c (rename=(DR1DAY=DR1DAY_IFF)) nh.dr1iff_d (rename=(DR1DAY=DR1DAY_IFF)); run;
The you submit
data nh.Dietaryday1_4yr; set nh.dr1iff_c (keep =seqn dr1ikcal DR1DAY_IFF ) nh.dr1iff_d (keep=seqn dr1ikcal DR1DAY_IFF); RUN;
Which recreates the data set from scratch. You did not rename the variable in the nh.dr1iff_c data set so you get the error.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.