I'm trying the code below and it is producing syntax error. Objective is to create one variable based on some condition. If the value not matches I want 'blanks' in the target variable. Any help?
Code which I used is,
data test1; set test; length branch1 $72.; if branch='2.7183E8' then branch1='271831234'; else if branch='2.7184E8' then branch1='271835678'; else missing ; run;
Log:
26 data test1; 27 set test; 28 length branch1 $72.; 29 if branch='2.7183E8' then branch1='271831234'; 30 else if branch='2.7184E8' then branch1='271835678'; 31 else missing _______ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 32 ; 33 run;
ELSE calls for a complete statement. The keyword MISSING on its own does not constitute a statement, you need to assign a missing value to branch1. Since branch1 is character, just use
branch1 = "";
or
call missing(branch1);
PS why do you keep obvious numbers in character variables? Or are these in fact identification codes that were stored as numbers accidentally somewhere in your process?
@David_Billa wrote:
Whether my if, else if and else part is synthetically correct?
???
SAS didn't show anything wrong with the other code so it is syntactically correct. Whether it is logically correct or meets the actual need depends on the data and the expected outcome.
You can clarify the value correction operations by coding a SELECT
statement instead of a long series of IF/THEN/ELSE
statements.
Example:
data want;
set sashelp.class;
select (name);
when ('Jane') student = 'Janie';
when ('James') student = 'Bond';
when ('Alfred') student = 'Batman';
otherwise; /* no statement because no correction needed */
end;
run;
In some cases you may want to use a custom format to make value corrections (i.e. transformations) during output.
proc format;
value $namefix(default=8) /* default width */
'Jane' = 'Janie'
'James' = 'Bond'
'Alfred' = 'Batman'
;
proc print data=sashelp.class;
where name in: ('A', 'J');
format name $namefix.;
run;
@David_Billa wrote:
I don't want to use the user defined format as the values in my data are constant. It will change frequently with new values which we can't predict beforehand.
Please re-read this post and pay attention to details. Is your data constant, or will it change frequently?
If you frequently need to add new values to a "change" lookup for correction, a custom format created from a dataset is the way to go. An addition then means adding a line with two words to a DATALINES block, not additional code in your main step.
Constantly editing code is problematic and troublesome.
A cleaner solution can be to maintain a table of corrections and use that for your branch1 assignments. New corrections are added to the process flow by adding new rows to the corrections table.
Example:
* day 1 corrections; data perm.corrections; length branch branch1 $15; input branch branch1; datalines; 2.7183E8 271831234 2.7184E8 271835678 ; * transforming SQL; proc sql; create table test1 as select have.*, corrections.branch1 as branch1 from test as have LEFT join perm.corrections on have.branch = corrections.branch ; * day 2; data new_corrections; length branch branch1 $15; input branch branch1; datalines; 2.7185E8 271851000 2.7186E8 271859998 ; proc append base=perm.corrections data=new_corrections; * same transforming SQL as before; proc sql; create table test1 as select have.*, corrections.branch1 as branch1 from test as have LEFT join perm.corrections on have.branch = corrections.branch ;
I am curious. Looks like you trying to correct for something that treated those branch ids is if they were numbers instead strings.
if branch='2.7183E8' then branch1='271831234';
else if branch='2.7184E8' then branch1='271835678';
Did you figure out what step in your overall process is generating this error in the strings?
It would make more sense to fix that instead of trying correct it later. You could get into the situation where the scientific notation values are the same for two different branches. How are you going to fix it then?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.