BookmarkSubscribeRSS Feed
rmes
Calcite | Level 5

Hi, 

 here is the dataset where all the value are store as single string.

 obs       info

    1        101 pencils 47
    2        102 parker pens 28
    3        103 apple ipod touch & shuffle 13
    4        104 dell studio laptop 03
  

I want first starting number under id and ending number under qty and rest under name. please help me to solve out this problem.

4 REPLIES 4
Shmuel
Garnet | Level 18

With hope I undertood your query:

 

data want;

    infile datalines;

    input info $60. ;

    id   = input(scan (info,1,' '), 3.);

    qty_x = scan (info,-1,' ');

    qty = input(qty_x,best6.);

    name = substr(info,5, length(trim(info) - 4 - length(qty_x)));

    drop qty_x;

run;

Reeza
Super User

Are you reading this from a text file or do you have it in a SAS dataset already?

Ksharp
Super User

data have;
infile cards truncover;
input  obs       info $80.;
cards;
    1        101 pencils 47
    2        102 parker pens 28
    3        103 apple ipod touch & shuffle 13
    4        104 dell studio laptop 03
;
run;
data want;
 set have;
 id=scan(info,1);
 name=prxchange('s/^\d+|\d+$//',-1,strip(info));
 qtr=scan(info,-1);
run;

Haikuo
Onyx | Level 15
data test;
infile cards truncover ;
input var $100.;
id=scan(var,1,'0123456789','k');
qty=scan(var,2,'0123456789','k');
name=scan(var,1,,'d');
cards;
101 pencils 47
102 parker pens 28
103 apple ipod touch & shuffle 13
104 dell studio laptop 03
;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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