libname cert xlsx 'C:\practice everyday\input\input08.xlsx';
libname results 'c:\cert';
data results.output08 (drop=province);
set cert.state;
province = scan(location,2,' ');
Location = tranwrd(Location,scan(location,2,' '),upcase(province));
run;
proc print data = results.output08;
run;
/* This program is replace in the same variable */
I want to place the province variable instead of the second argument of tranwrd function, but it doesn't work , why ?
@tianerhu wrote:
libname cert xlsx 'C:\practice everyday\input\input08.xlsx'; libname results 'c:\cert'; data results.output08 (drop=province); set cert.state; province = scan(location,2,' '); Location = tranwrd(Location,scan(location,2,' '),upcase(province)); run; proc print data = results.output08; run; /* This program is replace in the same variable */
I want to place the province variable instead of the second argument of tranwrd function, but it doesn't work , why ?
Try something along these lines:
data junk; location = 'Something gobbledygook and other words'; province = scan(location,2,' '); Location = tranwrd(Location,strip(scan(location,2,' ')),upcase(strip(province))); run;
What is happening is that the SCAN function is returning a value the length of your Location variable.
So when used in the Tranwrd function there are trailing blanks in the results that make the result long enough that the "words" after the Province no longer fit in the result.
If you use your original code creating a new variable instead of reusing Location it will work as well.
From the documentation for Tranwrd with some elements highlighted:
The TRANWRD function copies the value in source to the result string while searching for all non-overlapping substrings in source that are equal to the value in target. Each of these substrings is omitted from the result and the value of replacement is copied in its place. The TRANWRD function does not remove trailing blanks in the target string or the replacement string.
So you need to remove the trailing blanks returned by SCAN (in two places) and pay attention to the rules involved for creating new variables with some of these string functions.
What about it is not working? Show us.
the result:
the result should be the following:
the program is following:
libname cert xlsx 'C:\practice everyday\input\input08.xlsx';
libname results 'c:\cert';
data results.output08 (drop=province);
set cert.state;
province = scan(Location,2,' ');
Location = tranwrd(Location,province,upcase(province));
run;
proc print data = results.output08;
run;
Provide sample input data following these instructions
Do not provide us your Excel file, most of us will not download Microsoft Office files.
libname cert 'c:\cert\input';
Data cert.input09;
Input custID $;
Datalines;
ID1573
ID2369
ID5361
ID8902
;
Okay, I'm lost. The CUSTID given cannot produce the outputs you want.
Thank you for your help.
@tianerhu wrote:
libname cert xlsx 'C:\practice everyday\input\input08.xlsx'; libname results 'c:\cert'; data results.output08 (drop=province); set cert.state; province = scan(location,2,' '); Location = tranwrd(Location,scan(location,2,' '),upcase(province)); run; proc print data = results.output08; run; /* This program is replace in the same variable */
I want to place the province variable instead of the second argument of tranwrd function, but it doesn't work , why ?
Try something along these lines:
data junk; location = 'Something gobbledygook and other words'; province = scan(location,2,' '); Location = tranwrd(Location,strip(scan(location,2,' ')),upcase(strip(province))); run;
What is happening is that the SCAN function is returning a value the length of your Location variable.
So when used in the Tranwrd function there are trailing blanks in the results that make the result long enough that the "words" after the Province no longer fit in the result.
If you use your original code creating a new variable instead of reusing Location it will work as well.
From the documentation for Tranwrd with some elements highlighted:
The TRANWRD function copies the value in source to the result string while searching for all non-overlapping substrings in source that are equal to the value in target. Each of these substrings is omitted from the result and the value of replacement is copied in its place. The TRANWRD function does not remove trailing blanks in the target string or the replacement string.
So you need to remove the trailing blanks returned by SCAN (in two places) and pay attention to the rules involved for creating new variables with some of these string functions.
Thanks a lot.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.