BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasnewbie5
Fluorite | Level 6

Code is:

****

DATAstring;

    inputstring $10.;

DATALINES;

123nj76543

892NY10203

876pA83745

;

RUN;

****

 

How could I create a numeric variable called x that contains the first two digits of the string? How could I create a character variable called state that contains the fourth and fifth characters (uppercase)?

1 ACCEPTED SOLUTION

Accepted Solutions
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

data DATAstring;
length x 8.;
input inputstring $10.;
x = substr(inputstring,1,2);
state = upcase(substr(inputstring,4,2));
DATALINES;
123nj76543
892NY10203
876pA83745
;

View solution in original post

12 REPLIES 12
PeterClemmensen
Tourmaline | Level 20

Do like this

 

DATA string;
input string $10.;
DATALINES;
123nj76543
892NY10203
876pA83745
;
RUN;

data want;
   set string;
   var1=input(substr(string, 1, 2), best.);
   var2=upcase(substr(string, 4, 2));
run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

data DATAstring;
length x 8.;
input inputstring $10.;
x = substr(inputstring,1,2);
state = upcase(substr(inputstring,4,2));
DATALINES;
123nj76543
892NY10203
876pA83745
;

sasnewbie5
Fluorite | Level 6

Thanks. How do you differentiate between it being a character versus a numeric variable?

PeterClemmensen
Tourmaline | Level 20

See my answer above. I use the input function. 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

the length x 8. makes x numeric.

 

sasnewbie5
Fluorite | Level 6

Ohh ok, I understand now. Thanks guys!

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

did you question and needs get resolved?

Thanks is nice but the communities doesn't see that your needs have been fulfilled without you selecting a solution.

Please help the community by selecting and approving one of the replies.

 

sasnewbie5
Fluorite | Level 6

Good to know. I've accepted your solution VDD.

ballardw
Super User

Note that using the code provided by @VDD will cause a long entry like:

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).

which is not an error but sometimes indicates something odd has happened to your data or the code could use some cleaning.

 

 

That is why @PeterClemmensen used an INPUT to do the explicit conversion from character to numeric.

Jagadishkatam
Amethyst | Level 16

With perl regular expression you could also achieve the same as below 

 

DATA string;
    input string $10.;
	x=prxchange('s/(^\d{1,2})(\d\w*)/$1/i',-1,strip(string));
	state=prxchange('s/(\d?)//i',-1,upcase(strip(string)));

DATALINES;
123nj76543
892NY10203
876pA83745
;
RUN;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

So bored today

 

DATA string;
input string $10.;
DATALINES;
123nj76543
892NY10203
876pA83745
;
RUN;

data want;
set string;
x=peekclong(addrlong(string),2);
state=upcase(compress(string,,'kai'));
run;

 

 

data want;
set string;
x=peekclong(addrlong(string),2);
state=upcase(compress(string,,'d'));
run;

 

novinosrin
Tourmaline | Level 20
data want;
input x 1-2 @4 state $upcase2.;
cards;
123nj76543
892NY10203
876pA83745
;

sas-innovate-2024.png

Available on demand!

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

 

Register now!

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
  • 12 replies
  • 1212 views
  • 6 likes
  • 6 in conversation