DATA Step, Macro, Functions and more

substr

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

substr

hello:  why the following doesn't work?

data have;

input N1 best12.3;

cards;

542.

742.268

524.

742.39

;

run;

data want;

set have;

if N1='742' then substr(((put(N1,7.)), 1, 3) = '920';

run;

 

180 data want;

181 set have;

182 if N1='742' then substr(((put(N1,7.)), 1, 3) = '920';

                                                               -

                                                             79

                                              -

                                            356

ERROR 79-322: Expecting a ).

NOTE 356-185: The SUBSTR pseudo-variable function does not allow character constants, expressions, or

numeric constants for the first argument.

183 run;

 


Accepted Solutions
Solution
‎06-16-2017 10:36 AM
Super User
Posts: 5,503

Re: substr

I'll go with a slightly different variation:

 

if int(n1)=742 then n1 = n1 + 178;

 

That preserves the decimal fraction so increases from (for example) 742.123 to (for example) 920.123

 

SUBSTR only applies to character strings.  So if by chance N1 were actually a character variable, you could use:

 

if n1 =: '742.' then substr(n1, 1, 3) = '920';

View solution in original post


All Replies
Super User
Posts: 19,789

Re: substr

You have 4 open parentheses '(', and 3 closing parentheses ')' (like an earlier question). When you see that error, count the number of open/close parentheses. There are also keyboard shortcuts to jump to the matching closing/opening parentheses, but the error is telling you what's wrong, and exactly where. 

 

SUBSTR can be used in LEFT operation to replace text but I suspect that's not what you're trying to do. I don't know what you're trying to do though. 

 

if N1='742' then substr((put(N1,7.), 1, 3) = '920';

 

 

Super Contributor
Posts: 319

Re: substr

Try to change 742 to 920

Super User
Posts: 19,789

Re: substr


ybz12003 wrote:

Try to change 742 to 920


Then why are you using SUBSTR in your THEN?

Your condition will only be met if the number is exactly 742. 

 

I think your logic is backwards in your IF statement. I suggest literally deleting this one and trying to write your IF from scratch. 

 

 

 

Super User
Posts: 19,789

Re: substr

I think you want this:

 

if int(n1)=742 then n1= n1-int(n1) + 920;

You can do the character conversion. 

Super Contributor
Posts: 319

Re: substr

I still get the same error message even though I use if N1='742' then substr((put(N1,7.), 1, 3) = '920';

PROC Star
Posts: 7,471

Re: substr

Is this what you are trying to do?:

data have;
  input N1 best12.3;
  cards;
542.
742.268
524.
742.39
;
run;

data want;
  set have;
  if int(N1) eq 742 then n1 = 920;
run;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 19,789

Re: substr

@art297 It's 20 questions time, and I think I win Smiley Wink

PROC Star
Posts: 7,471

Re: substr

I beat you by 16 seconds! Smiley Happy

Super User
Posts: 19,789

Re: substr


art297 wrote:

I beat you by 16 seconds! Smiley Happy


Yeah, but your missing the decimal portion, and I think that's what he was trying to do, replace the integer portion. Mind you, it's still a guess....

Super Contributor
Posts: 319

Re: substr

No, I would like the final data is like

 

data have;

input N1 best12.3;

cards;

542.

920.268

524.

920.39

;

run;

PROC Star
Posts: 7,471

Re: substr

Then I agree with @Reeza's most recent suggestion.

 

@Reeza: You win!

 

Super Contributor
Posts: 319

Re: substr

I have other similar rows need to changed, something like 789.456 to 234.456.  Is there a way no need to be calculated?

Super User
Posts: 19,789

Re: substr


ybz12003 wrote:

I have other similar rows need to changed, something like 789.456 to 234.456.  Is there a way no need to be calculated?


Probably, but you'd have to tell us more. I'm tired of playing 20 questions tonight. 

Solution
‎06-16-2017 10:36 AM
Super User
Posts: 5,503

Re: substr

I'll go with a slightly different variation:

 

if int(n1)=742 then n1 = n1 + 178;

 

That preserves the decimal fraction so increases from (for example) 742.123 to (for example) 920.123

 

SUBSTR only applies to character strings.  So if by chance N1 were actually a character variable, you could use:

 

if n1 =: '742.' then substr(n1, 1, 3) = '920';

☑ This topic is solved.

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

Discussion stats
  • 17 replies
  • 313 views
  • 7 likes
  • 5 in conversation