BookmarkSubscribeRSS Feed
Alexxxxxxx
Pyrite | Level 9

Dear all,

 

How can I retain variable as character value?

 

when I running following codes,

%MACRO CompnayNameM(no=,Company_name=);
data step9.Patstat_total_hrm_Step34;
set step9.Patstat_total_hrm_Step34;
&COMPANY_NAME._S34=&COMPANY_NAME._S33;	
&COMPANY_NAME._S34=cat(' ',strip(&COMPANY_NAME._S34),' ');
/* GENERAL MULTILANGUAGE STANDARDIZATION (BY BHH)*/
if find(&COMPANY_NAME._S34,' A B ')>0 then do;
&Company_name._S34= tranwrd(&Company_name._S34,' A B ',' AB ');FlagSingle=1;end;
&Company_name._S34= tranwrd(&Company_name._S34,' A CALIFORNIA CORP ',' CORP ');
&Company_name._S34= tranwrd(&Company_name._S34,' A DELAWARE CORP ',' CORP ');
&Company_name._S34= tranwrd(&Company_name._S34,' AKTIEBOLAGET ',' AB ');
..

&Company_name._S34= tranwrd(&Company_name._S34, ' CURATORS ',' ');FlagCLEAN_UNI_NAME=1;end; /* GM */ 
if find(&Company_name._S34,' THE ' )>0 then do;
&Company_name._S34= tranwrd(&Company_name._S34, ' THE ',' ');FlagCLEAN_UNI_NAME=1; FlagTHE=1;end;

&Company_name._S34= tranwrd(&Company_name._S34, '   ',' ');
&Company_name._S34= tranwrd(&Company_name._S34, '  ',' ');
&Company_name._S34=strip(&Company_name._S34);
c&Company_name._33v34=(&Company_name._S34 ne &Company_name._S33);
run;

%MEND CompnayNameM;
%CompnayNameM(no=1,Company_name=HRM_L2)
%CompnayNameM(no=2,Company_name=PERSON_NAME)
...
%CompnayNameM(no=11,Company_name=HRM_L2_B)
%CompnayNameM(no=12,Company_name=PERSON_NAME_B)
run;

I got the result like 

6759  %CompnayNameM(no=1,Company_name=HRM_L2)

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
      1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1   1:1
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      439:21    439:150   441:54    441:211   442:124   443:26    443:177   445:73    445:220
      446:114   447:21    447:162   449:60    449:194   450:85    450:219   451:101   451:236
      453:132   454:21    454:155   455:50    455:200   457:98    457:244   458:139   459:30
      459:174   461:64    461:204   462:88    462:226   463:110   463:244   465:132   466:21
      466:157   467:46    467:188   469:75    469:213   470:105   470:238   471:133   473:21
      473:161   474:62    474:198   475:90    475:232   477:135   478:21    478:157   479:41
      479:173   481:62    481:198   482:84    482:221   483:109   483:247   485:131   486:21
      486:153   487:30    487:166   489:63    489:201   490:86    490:220   491:100   491:238
      493:139   494:26    494:166   495:60    495:192   497:68    497:207   498:115   499:21
      499:163   501:63    501:215   502:132   503:55    503:215   505:127   506:30    506:184
      507:81    507:231   509:143   510:47    510:191   511:96    511:242   513:137   514:26
      514:164   515:61    515:195   517:82    517:220   518:119   519:21    519:173   521:73
      521:221   522:115   523:21    523:163   525:63    525:201   526:86    526:228   527:120
      529:21    529:163   530:65    530:207   531:109   531:247   533:131   534:21    534:153
      535:30    535:162   537:44    537:182   538:68    538:210   539:115   541:21    541:176
      542:67    542:214
NOTE: There were 113707 observations read from the data set STEP9.PATSTAT_TOTAL_HRM_STEP34.
NOTE: The data set STEP9.PATSTAT_TOTAL_HRM_STEP34 has 113707 observations and 50 variables.
NOTE: DATA statement used (Total process time):
      real time           4:45.73
      cpu time            3:05.20

I really confused about how to retain all variable as a character value rather than automatically convert to a numeric value? Could you please give me some suggestions about this?

 

3 REPLIES 3
heffo
Pyrite | Level 9

It's hard to know if you have any variables that are numeric, but you treat as strings in the table and the opposit. So, first you can verify that the variables &COMPANY_NAME._S34, &COMPANY_NAME._S33 and c&Company_name._33v34 are strings. And that the variables FlagSingle, FlagCLEAN_UNI_NAME and FlagTHE are numeric. 

I can't see what you are doing with the macro input variable No.

Try using 

options mprint mlogic symbolgen;

Btw, should it be c&Company_name._33v34? that is, should the c be infront of the company name? 

 

Another way to find the culprits would be to temporarily remove half the code it to try to find the row that gives the error. Then remove half of the code again and so on, till you find it! 🙂

 

Also, I would probably use the compbl(stringVar) function, it removes multiple blanks from a character string. So, no need to have double checks for double and triple spaces. 

 

ScottBass
Rhodochrosite | Level 12

Remove the macro bits:

 

 

data step9.Patstat_total_hrm_Step34;
set step9.Patstat_total_hrm_Step34;
&COMPANY_NAME._S34=&COMPANY_NAME._S33;	
&COMPANY_NAME._S34=cat(' ',strip(&COMPANY_NAME._S34),' ');
/* GENERAL MULTILANGUAGE STANDARDIZATION (BY BHH)*/
if find(&COMPANY_NAME._S34,' A B ')>0 then do;
&Company_name._S34= tranwrd(&Company_name._S34,' A B ',' AB ');FlagSingle=1;end;
&Company_name._S34= tranwrd(&Company_name._S34,' A CALIFORNIA CORP ',' CORP ');
&Company_name._S34= tranwrd(&Company_name._S34,' A DELAWARE CORP ',' CORP ');
&Company_name._S34= tranwrd(&Company_name._S34,' AKTIEBOLAGET ',' AB ');
..

&Company_name._S34= tranwrd(&Company_name._S34, ' CURATORS ',' ');FlagCLEAN_UNI_NAME=1;end; /* GM */ 
if find(&Company_name._S34,' THE ' )>0 then do;
&Company_name._S34= tranwrd(&Company_name._S34, ' THE ',' ');FlagCLEAN_UNI_NAME=1; FlagTHE=1;end;

&Company_name._S34= tranwrd(&Company_name._S34, '   ',' ');
&Company_name._S34= tranwrd(&Company_name._S34, '  ',' ');
&Company_name._S34=strip(&Company_name._S34);
c&Company_name._33v34=(&Company_name._S34 ne &Company_name._S33);
run;

 

 

Do a search and replace of "&Company_name" with one of your representative values, such as "HRM_L2".

 

Resubmit - you'll get better line and column numbers in the log indicating the problem statements.

 

More than likely your problem variables are not contained in your source dataset and are defaulting to numeric values.  If that is the case, declare them as character via length or attrib statements.  Use the PUT() and INPUT() functions to explicitly convert character to numeric and vice versa.

 

Fully debug your SAS code, then convert to macro.

 

Along those lines:

 

1) You declare no= as a macro parameter but never reference it in your code.

2) Minor: I assume the macro name CompnayNameM is misspelled.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
ballardw
Super User

FWIW, RETAIN is a specific statement in SAS regarding keeping values from one observation to the next in a data step.

Since that is not the context you are using the word retain it would be better to come up with a better phrase to avoid possible confusion.

 

You do not mention which "variable" is supposed to be "retained" as character.

 

Please note that every time you use this construct:

data step9.Patstat_total_hrm_Step34;
set step9.Patstat_total_hrm_Step34;

where the output data set and one on the SET statement is the same that you completely rewrite the data set. So if you have "reused" this data set such as calling the macro multiple times there is no way to know when you may have first changed a variable from character to numeric (if you did) or completely removed a variable from the data set or created a numeric variable because you reference a variable that had not actually previously existed.

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
  • 3 replies
  • 1168 views
  • 0 likes
  • 4 in conversation