BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9
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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.

 

 

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

What about it is not working? Show us.

--
Paige Miller
tianerhu
Pyrite | Level 9

the result:

tianerhu_0-1623194558016.png

the result should be the following:

tianerhu_1-1623194658794.png

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;

PaigeMiller
Diamond | Level 26

Provide sample input data following these instructions

 

Do not provide us your Excel file, most of us will not download Microsoft Office files.

--
Paige Miller
tianerhu
Pyrite | Level 9

libname cert 'c:\cert\input';

Data cert.input09;

Input custID $;

Datalines;

ID1573

ID2369

ID5361

ID8902

;

PaigeMiller
Diamond | Level 26

Okay, I'm lost. The CUSTID given cannot produce the outputs you want.

--
Paige Miller
tianerhu
Pyrite | Level 9

Thank you for your help.

ballardw
Super User

@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.

 

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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