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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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; 

 

--
Paige Miller

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

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;
PG
knighsson
Obsidian | Level 7
Thank you!
PaigeMiller
Diamond | Level 26

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; 

 

--
Paige Miller
knighsson
Obsidian | Level 7

That makes sense!

Thank you!

 

Tom
Super User Tom
Super User

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;

 

knighsson
Obsidian | Level 7
Thank you!
ballardw
Super User

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.

knighsson
Obsidian | Level 7
Thank you!

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 1255 views
  • 5 likes
  • 5 in conversation