Learning SAS? Welcome to the exclusive online community for all SAS learners.

Variable Name in SAS Studio

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Variable Name in SAS Studio

There is something basic that is escaping me here, undoubtedly:

 

In SAS Studio, I keep getting this error message in the log, at the last data step below, when creating file WORK.Part_31 

 

"ERROR: Variable RLD_A is not on file WORK.PART_3.

ERROR: Variable RLD_B is not on file WORK.PART_3.
ERROR: Variable RLD_C is not on file WORK.PART_3."

 

Looking at the SAS datafile WORK.PART_3, the variables named RLD_A, RLD_B, RLD_C all exist, and the SAS datafile name is correct.

So what simple item am I overlooking?  Thanks

 

Here is my code:

 

data Part_1 ;                                 infile datalines dsd;
input d Product $ Diameter;
datalines;
0.1, RLD, 3.251,
0.1, RLD, 3.473,
0.1, RLD, 3.602,
0.5, RLD, 9.322,
0.5, RLD, 9.185,
0.5, RLD, 8.822,
0.9, RLD, 20.454,
0.9, RLD, 20.512,
0.9, RLD, 19.411,
0.1, Test, 3.543,
0.1, Test, 3.742,
0.1, Test, 3.558,
0.5, Test, 7.753,
0.5, Test, 7.64,
0.5, Test, 7.786,
0.9, Test, 15.016,
0.9, Test, 14.22,
0.9, Test, 15.054,
;
run ;

Data Part_15 ; set Part_1 ;
d_pct = "AAAAA" ;
if d = 0.1 then d_pct = "A" ;
if d = 0.5 then d_pct = "B" ;
if d = 0.9 then d_pct = "C" ;
Prod_d = catx('_', Product, d_pct) ;
run ; Proc sort ; by Prod_d ; run ;

 

/* start PROC Transpose and rename variables */
proc transpose data=Part_15 out=Part_2;
by Prod_d;
var Diameter;
run;
proc transpose data= Part_2 out=Part_3(drop=_name_);
id Prod_d;
run;

 

data Part_31 ; set Part_3
(rename=(RLD_A=A RLD_B=B RLD_C=C)) ;
Drop RLD_A RLD_B RLD_C ;
run ;


Accepted Solutions
Solution
13 hours ago
Occasional Contributor
Posts: 15

Re: Variable Name in SAS Studio

I rewrote sections of the code to remove the transpose step, and that fixed the variable name problems. Still not exactly sure what the problem was. Thank you all for you help and sage advice.

View solution in original post


All Replies
Super User
Posts: 19,058

Re: Variable Name in SAS Studio

You've renamed the variables in the SET statement. So they're brought in with different names. You can't refer to them using the old names then, you need to use the new names.

 

Your RENAME is a data set option, but your DROP is a data step statement

Occasional Contributor
Posts: 15

Re: Variable Name in SAS Studio

Thank you, and good point. 

But when I remove the data set option DROP step, the same error message appears, and there is no new dataset generated "Part_31".

In the SAS file "Part_3" there are variables names RLD_A, RLD_B, RLD_C, .....

So why won't SAS rename them?

 

 

Super User
Posts: 19,058

Re: Variable Name in SAS Studio


rmacarthur wrote:

Thank you, and good point. 

But when I remove the data set option DROP step, the same error message appears, and there is no new dataset generated "Part_31".

In the SAS file "Part_3" there are variables names RLD_A, RLD_B, RLD_C, .....

So why won't SAS rename them?

 

 


Your code above runs fine when this change is made.

Do you have something else not shown?

Super User
Super User
Posts: 6,842

Re: Variable Name in SAS Studio

[ Edited ]

Check your log and make sure that the step that is create PART_3 is actually running.

If that step failed then you might be using an earlier version of PART_3 that does not have those variables.

Also since the variables are being created based on the values in the data perhaps the data you are actually using do not have any records that would create those variables.

 

Note that PROC TRANSPOSE will generate names from multiple ID variables for you.  And if you had a BY variable you could eliminate one of those transpose steps.

 

proc sort data=part_15 out=test1;
  by product d_pct;
run;

data test2;
  do row=1 by 1 until (last.d_pct) ;
    set test1;
    by product d_pct  ;
    output;
  end;
run;
proc sort data=test2;
  by row ;
run;
proc transpose data=test2 out=test3 (drop=_name_) delimiter=_;
  by row ;
  id d_pct product ;
  var diameter ;
run;

image.png

 

Also instead of dropping the variable you could just eliminate the rows going into PROC TRANSPOSE to prevent the variable from every being created.

proc transpose data=test2 out=test4 (drop=_name_) delimiter=_;
  where d_pct ne 'B';
  by row ;
  id d_pct product ;
  var diameter ;
run;

image.png

Occasional Contributor
Posts: 15

Re: Variable Name in SAS Studio

Thank you very much.  

The SAS log indicates that the SAS data file WORK.Part_3 is indeed being generated without any error.

I inserted your code (removing one of the transpose steps) into the code and it runs well, but the same error occurs.

 

The problem may have to do with SAS naming the variables in a transpose step.  I am using variable names taken right from the SAS file (in SAS Studio), but SAS log continues to indicate:

 

"ERROR: Variable A_RLD not found"

 

, etc....

 

Super User
Posts: 19,058

Re: Variable Name in SAS Studio

Post your exact code and log.


rmacarthur wrote:

Thank you very much.  

The SAS log indicates that the SAS data file WORK.Part_3 is indeed being generated without any error.

I inserted your code (removing one of the transpose steps) into the code and it runs well, but the same error occurs.

 

The problem may have to do with SAS naming the variables in a transpose step.  I am using variable names taken right from the SAS file (in SAS Studio), but SAS log continues to indicate:

 

"ERROR: Variable A_RLD not found"

 

, etc....

 


 

Super User
Super User
Posts: 6,842

Re: Variable Name in SAS Studio

[ Edited ]

So if you have a dataset named XXX that should have a variable named YYY and when you run code like this:

data new ;
  set xxx;
  drop yyy;
run;

Then that variable is NOT in that dataset.

Perhaps you real variable name is something else. You could be looking at the variable's LABEL instead of the variable NAME.

You could have set the option validvarname=ANY in which case perhaps your variable actually has a space in its name or some other non-printing character.

Run proc contents on the data set. Try generating a name literal from the name of the variable.

proc contents data=xxx out=contents noprint; run;
data contents;
  length nliteral $40 ;
  set contents(keep=name type length label);
  nliteral=nliteral(name);
run;
proc print;
run;

 

Solution
13 hours ago
Occasional Contributor
Posts: 15

Re: Variable Name in SAS Studio

I rewrote sections of the code to remove the transpose step, and that fixed the variable name problems. Still not exactly sure what the problem was. Thank you all for you help and sage advice.
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 166 views
  • 2 likes
  • 3 in conversation