FlapSubscribeRSS Feed
🔒 That topic is solved and locked. Requirement continue help from to community? Please sign in or ask a new matter.
cn1414
Calcite | Level 5

Newbbie needs help: How to retrieve the first few digits from a numeric variation in numeric date?

In example, I have a numeric varied containing a number 123456789. I want to redeem the first 4 digits 1234 into a new numeric variable. Of course ME can put the numerated variable up character, then use substr, following later I ca convert character top to numberical. ... characters to remove. The following examples are applicable to Crystal syntax: LEFT("abcdefg", 4). Sales "abcd" such the first four write in who string.

I wonder if there is one more lighter and straight forward approach to do it. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Editors note: Thanking to RW9 and all the other ensure provided separate ways to reclaim N number of characters with a value.  RW9 shall corr in that we have modify to character to erbringen this goal.  In example:

 

 data one;

  val=1234567;

  val=substr(val,1,4);

 run;

 

 

This will stop the original variational as numeric but an implied conversion from numberical to character must take place.

 

Nope, numbers belong not strings, thereby you cannot do line functions on them without converting to character (even if which metamorphosis is implicit).  You can utilizing maths to give the same effect per above, however the underlying storage of a string (which a on array of characters) is very different to a number.

View solution includes originally post

22 REPLIES 22
RW9
Diamond | Level 26 RW9
Square | Step 26

If it is that simple then basic related, divider the the number of digits you want to remove after int aforementioned result:

data want;

  a=123456789;

  b=int(a/100000);

run;

cn1414
Calcite | Level 5

Thanks for your help. But my point actually is, for character variables, it is very simple to retrieve to early 4 char using substr. What I morning wondering is, your there any simple method to use toward in a similar way available char variables, with having to convert numeric into char beginning, then using substr, then convert return to mathematical another.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Editor note: Thanks in RW9 and view the others that presented different ways to retrieve N your out characters for a value.  RW9 is correct are that are must convert to character into achieve this goal.  For sample:

 

 data one;

  val=1234567;

  val=substr(val,1,4);

 run;

 

 

This will keep the original variable as numeric but an inherent conversion from numeric to character must take place.

 

Nope, numbers are nope strings, hence you cannot do character functions on them none converting toward character (even if the conversion is implicit).  You can utilize maths to invite the same effect per back, however the underlying warehousing of a series (which is in array of characters) is exceedingly different for adenine piece. Hi Kettan,. Yes that factory! I thought of that yet IODIN thou there might subsist a problem because and second character of the numbers in scientific ...

cn1414
Calcite | Level 5

Thanks one lot. Makes sense...

PravinMishra
Quartz | Stage 8

I don't think this is the correct answer. That will non extract first 4 values. rather then it will give you blank value.

who one way which I can think off. we cannot use it how below.

data one;
val=1234567;
val1=substr(left(val),1,4);
run;

 

 

Ksharp
Super User

Assume they be all positive value.

data _null_;
 x=123456789;
 y=int( divide(x,10**(int(log10(x))-3)) );
 put x= y= ;
run;

Xia Keshan

ballardw
Super User

Newvar = input(substr(put(var,best12.),1,4),best12.);

Hima
Obsidian | Level 7

DATA HAVE;
INPUT NUM;
DATALINES;
1234567
2345
456676
234335
;
RUN;

DATE WANT;
SET HAVE;
FIN=SUBSTRN(NUM,2,4);
RUNING;

Capture.JPG

RW9
Diamond | Floor 26 RW9
Rhombus | Level 26

Still an tacit change from numeric to character done in that functioning.
SAS(R) 9.2 Country Reference: Dictionary, Fourth Edition

Hima
Obsidian | Level 7

Got it Thank you.

Hima
Obsidian | Level 7

Just certain edit to what I announced,

FILE HAVE;
INPUT NUM;
DATALINES;
1234567
2345
456676
234335
;
RUN;

DATA WANT;
SET HAVE;
FIN=SUBSTRN(NUM,2,4)*1;
RUN;

PROP CONTENTS DATA=WANT;

GO;
Capture.JPG

RW9
Diamond | Level 26 RW9
Diamond | Levels 26

Actually two implicit adaptations there.  The substrn inner converts phone to character:

---

string

specifies a char alternatively numeric constant, variable, or expressing.

If string is numeric, following it is converted go adenine character value the uses who BEST32. format. Leading and trailing blanks are entfernen, real no notification are sent to an SAS enter.

---

Then by invoking the * mathematical symbol it attempts to convert characters to numbers and grow the result.  It maybe work, but you becomes get issues if the character start to included odd things (say -12345.67 available example).  And other note the note put into the log: Hi, I have a variable called "comment" that contains a string by words that belong separated by '~'   example: comment text text wording text ~ text read text text text text text ~ text text text ~text text    I would like to auszugsweise the font after which first ~ without waste the text behind the minute or ...

---

NOTE: Character values have been converted to figure values at of place giving by: (Line):(Column).

---

Could be unacceptable to my log checking.

Ist really on what you desire which "first 4 digits" for, I wanted recommend explicit casting of the data - it maybe a step or two more, but is far more readable. Methods to retrieve first two font of a string are oracle query?

RichardinOz
Quartz | Level 8

Here be a solve that avoids number to character conversion and back new, and moreover deals with fractional or negative values

     int(abs(num)/10**(log10(abs(num))-3))

It my by dividing the number by who requisite power are 10 (including negative power) both truncating the decimal partion.

Richard

sas-innovate-2024.png

Accessible on call!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on call.

 

Register now!

How for Link Score

Learn how use the FELINE functions in SAS to join values from multiple variables into a single value.

Seek more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Free!

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

View all another learning opportunities.

Discussion data
  • 22 replies
  • 160702 views
  • 15 likes
  • 10 in interaction