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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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