DATA Step, Macro, Functions and more

combine numeric&string to new var

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

combine numeric&string to new var

I have a data set that need to be updated. 
Id 
89
180
2003
39123
I need to create a new string var "Unique_A200_2018" based on the id# and A200_2018. So, for id 89, his new Unique_A200_2018 is A200_2018_89.  id 39123, the new Unique_A200_2018 is A200_2018_39123 and so on.
New in SAS world, please help. Thanks, Shirley.


Accepted Solutions
Solution
‎05-14-2018 09:31 AM
Respected Advisor
Posts: 4,797

Re: combine numeric&string to new var

Posted in reply to Bintang18

@Bintang18 wrote:

This is what I got. I know, I have to fix  var property, but how about the invalid argument. Did I miss something?

BTW, id is numeric

Thank you again.


WARNING: In a call to the CATT function, the buffer allocated for the result
was not long enough to contain the concatenation of all the arguments.
The correct result would contain 12 characters, but the actual result
might either be truncated to 1 character(s) or be completely blank,
depending on the calling environment. The following note indicates the
left-most argument that caused truncation.


NOTE: Argument 1 to function CATT('A200_2018_',78) at line 40 column 22 is
invalid.


@Bintang18

The CATT() function creates a string which you then assign to a variable (the variable left from the equal sign).

If this variable is not long enough to hold the generated string then you get a log message as above.

 

Add at the very beginning of your data step a LENGTH statement  defining a length long enough for the generated string.

data have;
  id=25;
run;

data want;
  length newvar $20;
  set have;
  newvar=catt('A200_2018_',id);
run;

 

View solution in original post


All Replies
Super User
Posts: 8,216

Re: combine numeric&string to new var

Posted in reply to Bintang18

There's a function for that! e.g.:

data want;
  set have;
  Unique_A200_2018=catt('A200_2018_',id);
run;

Art, CEO, AnalystFinder.com

 

New Contributor
Posts: 3

Re: combine numeric&string to new var

This is what I got. I know, I have to fix  var property, but how about the invalid argument. Did I miss something?

BTW, id is numeric

Thank you again.


WARNING: In a call to the CATT function, the buffer allocated for the result
was not long enough to contain the concatenation of all the arguments.
The correct result would contain 12 characters, but the actual result
might either be truncated to 1 character(s) or be completely blank,
depending on the calling environment. The following note indicates the
left-most argument that caused truncation.


NOTE: Argument 1 to function CATT('A200_2018_',78) at line 40 column 22 is
invalid.

Super User
Posts: 8,216

Re: combine numeric&string to new var

Posted in reply to Bintang18

You haven't told us something, but I don't know what that something is. The following worked for me:

data have;
  input Id;
  cards; 
89
180
2003
39123
;

data want;
  set have;
  Unique_A200_2018=catt('A200_2018_',id);
run;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 13,941

Re: combine numeric&string to new var

Posted in reply to Bintang18

@Bintang18 wrote:

This is what I got. I know, I have to fix  var property, but how about the invalid argument. Did I miss something?

BTW, id is numeric

Thank you again.


WARNING: In a call to the CATT function, the buffer allocated for the result
was not long enough to contain the concatenation of all the arguments.
The correct result would contain 12 characters, but the actual result
might either be truncated to 1 character(s) or be completely blank,
depending on the calling environment. The following note indicates the
left-most argument that caused truncation.


NOTE: Argument 1 to function CATT('A200_2018_',78) at line 40 column 22 is
invalid.


Did you copy and paste code from the forum? If the code is not in a code box, such as this opened with the {I}

something = CATT('A200_2018_',var);

or the box from the "running man" icon:

 

something = CATT('A200_2018_',var);

 

you might have accidentally picked up some invisible html or similar code. If you did copy/paste then try deleting the line of code and retyping.

I ran into one example on this site where a block of datalines had about one instance of "hidden" characters per line. Went through a lot of run the data step, read the errors, retype a data value. Repeat. The editor did not give any indication what those values might have been, there were "invisible" as far as the editor was concerned.

Solution
‎05-14-2018 09:31 AM
Respected Advisor
Posts: 4,797

Re: combine numeric&string to new var

Posted in reply to Bintang18

@Bintang18 wrote:

This is what I got. I know, I have to fix  var property, but how about the invalid argument. Did I miss something?

BTW, id is numeric

Thank you again.


WARNING: In a call to the CATT function, the buffer allocated for the result
was not long enough to contain the concatenation of all the arguments.
The correct result would contain 12 characters, but the actual result
might either be truncated to 1 character(s) or be completely blank,
depending on the calling environment. The following note indicates the
left-most argument that caused truncation.


NOTE: Argument 1 to function CATT('A200_2018_',78) at line 40 column 22 is
invalid.


@Bintang18

The CATT() function creates a string which you then assign to a variable (the variable left from the equal sign).

If this variable is not long enough to hold the generated string then you get a log message as above.

 

Add at the very beginning of your data step a LENGTH statement  defining a length long enough for the generated string.

data have;
  id=25;
run;

data want;
  length newvar $20;
  set have;
  newvar=catt('A200_2018_',id);
run;

 

New Contributor
Posts: 3

Re: combine numeric&string to new var

Thank you all for helping me out, it worked after I edit var length. Shirley
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 142 views
  • 0 likes
  • 4 in conversation