BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thebugslayer
Fluorite | Level 6
Hello. Don't ask me how I landed on SAS, but it's now on my plate so I am learning...
In my data plan, I am trying need to add a column to an existing dataset. The way I found to do it is by adding a column in a code transform. However, I am struggling to create a variable then assign that variable to the new column. I have looked at a few articles and I feel I am pretty close. As a matter of fact one of the iterations worked but I did not save it before. The error I am getting with this code is:

The action was not successful
ERROR: Expecting an arithmetic operator.
ERROR: Syntax error, statement will be ignored.
ERROR: Statement is not valid or it is used out of proper order.
ERROR: Expecting an arithmetic operator.  (occurred 16 times)
ERROR: Syntax error, statement will be ignored.  (occurred 16 times)
ERROR: Statement is not valid or it is used out of proper order.  (occurred 16 times)
 
Here is my code:
/* BEGIN data step with the output table data */
data {{_dp_outputTable}} (caslib={{_dp_outputCaslib}} );
  /* Set the input set */
  set {{_dp_inputTable}} (caslib={{_dp_inputCaslib}} );
  length _renamed $50;
  if LabName = 'WESTERN PATHOLOGY CONSULTANTS' THEN
    _renamed = 'RENO' ELSE _renamed = LabName;
  "Laboratory_Renamed" = _renamed;
    /* END data step run */
run;

What am I doing wrong?
Once the column is added, what happens the next time I run the data plan, is it dropped and recreated automatically?
 
Thank you!
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA), Training HOTT cert SQL Server 2012 Business Intelligence (SSIS, SSAS).
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

I think you need a semicolon before the ELSE.

 

/* BEGIN data step with the output table data */
data {{_dp_outputTable}} (caslib={{_dp_outputCaslib}} );
  /* Set the input set */
  set {{_dp_inputTable}} (caslib={{_dp_inputCaslib}} );
  length _renamed $50;
  if LabName = 'WESTERN PATHOLOGY CONSULTANTS' THEN
    _renamed = 'RENO'; 
  ELSE _renamed = LabName;
  Laboratory_Renamed = _renamed;
    /* END data step run */
run;
Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

View solution in original post

5 REPLIES 5
SuryaKiran
Meteorite | Level 14

Hello,

 

If your trying to assign values for _renamed to a new column then you must have the statement as :

 

Laboratory_Renamed = _renamed;

If you put something in quotes, that means your assigning a string value to the variable. 

Thanks,
Suryakiran
thebugslayer
Fluorite | Level 6

Thank you @SuryaKiran .

 

I fixed the line but I still am getting

The action was not successful
ERROR: Expecting an arithmetic operator.
ERROR: Syntax error, statement will be ignored.
ERROR: Expecting an arithmetic operator.  (occurred 16 times)
ERROR: Syntax error, statement will be ignored.  (occurred 16 times)

ChrisHemedinger
Community Manager

I think you need a semicolon before the ELSE.

 

/* BEGIN data step with the output table data */
data {{_dp_outputTable}} (caslib={{_dp_outputCaslib}} );
  /* Set the input set */
  set {{_dp_inputTable}} (caslib={{_dp_inputCaslib}} );
  length _renamed $50;
  if LabName = 'WESTERN PATHOLOGY CONSULTANTS' THEN
    _renamed = 'RENO'; 
  ELSE _renamed = LabName;
  Laboratory_Renamed = _renamed;
    /* END data step run */
run;
Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
thebugslayer
Fluorite | Level 6

Thank you @ChrisHemedinger !

 

That was it. Interesting paradigm. I was thinking like C# or most other programming languages where you don't have to put the only statement of a THEN branch in a block. 

 

Another misconception of mine is that in my code, I expected Laboratory_Renamed to be the new column but it was simply treated as an assignment and ignored. _renamed was instead the column. 

 

Here is the final version working version:

 

/* BEGIN data step with the output table data */
data {{_dp_outputTable}} (caslib={{_dp_outputCaslib}} );
  /* Set the input set */
  set {{_dp_inputTable}} (caslib={{_dp_inputCaslib}} );
  length Laboratory_Renamed $50;
  if LabName = 'WESTERN PATHOLOGY CONSULTANTS' then
    Laboratory_Renamed = 'RENO';
  else Laboratory_Renamed = LabName;
  /*Laboratory_Renamed = _renamed;*/
    /* END data step run */
run;

 

PS: Note to self: learn to program in SAS.

 

 

ChrisHemedinger
Community Manager

Welcome to SAS!  Here, we get paid by the semicolon --- so bring them on! Smiley Happy

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

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
  • 5 replies
  • 744 views
  • 5 likes
  • 3 in conversation