Hello:
I have the following dataset. After I import them into SAS, the variable "coverage" become characterized. I would like to replace the asterisk * with -99. I wrote a quote below. But one error message was shown in the log page, It said that the "Coverage1" was uninitiated. Why won't it happen? How to resolve it?
Jurisdiction-Level Coverage
Alabama 3
Alaska 1
Arizona 3
Arkansas 3
California 1
Colorado 1
Connecticut 1
Delaware 1
District of Columbia 1
Florida *
Georgia *
459 data test (drop=coverage);
460 length coverage $4.;
461 set test;
462 coverage=tranwrd(coverage1,'*','-99');
463 coverage=input(coverage1, 4.);
464 run;
NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
462:33 463:25 463:31
NOTE: Variable coverage1 is uninitialized.
You can't have "Coverage" as a character and a numeric variable in the same data step.
What I do in this situation is rename the incoming variable in the "set" statement, something like:
set test(rename=(coverage=coverage_char));
and then
coverage_char=tranwrd(coverage_char,'*','-99');
coverage=input(coverage_char, 4.);
or
coverage=input(tranwrd(coverage_char,'*','-99'), 4.);
(untested)
Tom
You can't have "Coverage" as a character and a numeric variable in the same data step.
What I do in this situation is rename the incoming variable in the "set" statement, something like:
set test(rename=(coverage=coverage_char));
and then
coverage_char=tranwrd(coverage_char,'*','-99');
coverage=input(coverage_char, 4.);
or
coverage=input(tranwrd(coverage_char,'*','-99'), 4.);
(untested)
Tom
data test (drop=coverage);
/* the drop means that the variable "coverage" COMING FROM THE DATA STEP will not be included in the output dataset; this makes the whole data step useless */
length coverage $4.;
set test;
coverage=tranwrd(coverage1,'*','-99');
coverage=input(coverage1, 4.);
run;
You probably wanted this:
data test (drop=coverage1 coverage2);
set test (rename=(coverage=coverage1));
coverage2=tranwrd(coverage1,'*','-99');
coverage=input(coverage2, 4.);
run;
I used coverage2 because you can't rely on coverage1 (the old coverage) being long enough to hold the '-99' string.
Here is another approach using a custom informat.
proc format library=work;
invalue da
'*'=-99
other=_same_;
run;
data example;
informat x $5.;
input x ;
coverage = input(x,da.);
datalines;
1
2
3
4
*
;
run;
You still need to have the resulting variable with a different name than that existing in you current data though.
If you were to end up having multiple codes you needed clean such as "**" then the Tranward approach requires multiple lines of code and order is going to be important (you wouldn't want to replace ** with -99-99 if the intent was ** maps to -999). The format approach would allow adding multiple replacements in the informat construction (not to mention multiple values to the same result) and only one line of code in the manipulation step.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.