seperate a single column values to each varibles

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

seperate a single column values to each varibles

hi i have a simple data set then how can i separate the data set values to each variable like dep_id ,dept_name,dep_qty.Please help ?

data a;

input info $1 - 50;

cards;

101 pencil 57

102 parker pen 24

103 hp studion shuffle n 10

104 samsung galexy ace 29

run;

i already solved the problem.But i need to know any easy way of doing the task simply.

i put my code on below..Please do check it and make some replies

data b;

set a;

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

xyz=substr(info,5);

pdt_nam=length(substr(info,5));

name=input(substr(xyz,1,pdt_nam-2),$20.);

pdt_qty=input(substr(xyz,anydigit(xyz)),2.);

label  pdt_id=Product Id

       name=Product Name

       pdt_qty=Product Quantity;

drop pdt_nam;

drop info;

drop xyz;

runn;

Krishnachandran


Accepted Solutions
Solution
‎07-22-2014 09:48 AM
Super User
Posts: 10,041

Re: seperate a single column values to each varibles

Posted in reply to KrishnaChandra

How about this :

data a;
input info $ 1-50;
cards;
101 pencil 57
102 parker pen 24
103 hp studion shuffle n 10
104 samsung galexy ace 29
;
run;
data b;
 set a;
 dep_id =scan(info,1,' ');
 dept_name=substr(info,findc(info,' ')+1,findc(trim(info),' ','b') - length(dep_id)-1 );
 dep_qty=substr(info,findc(trim(info),' ','b')+1);
run;

OR this one :

data a;
input info $ 1-50;
cards;
101 pencil 57
102 parker pen 24
103 hp studion shuffle n 10
104 samsung galexy ace 29
;
run;
data b;
 set a;
 dep_id =scan(compress(info,' ','kd'),1,' ');
 dept_name=compress(info,' ','ka');
 dep_qty=scan(compress(info,' ','kd'),-1,' ');
run;

Xia Keshan

Message was edited by: xia keshan

View solution in original post


All Replies
Super User
Posts: 7,833

Re: seperate a single column values to each varibles

Posted in reply to KrishnaChandra

You might want to introduce a dedicated delimiter into the input data, like the colon. Then you can use the dlm= option in the infile statement and let the input statement do the separation for you.

data want;

infile cards dlm=':'

format

  pdt_id 3.

  pdt_name $20.

  pdt_quantity 2.

;

input

  pdt_id

  pdt_name

  pdt_quantity

;

datalines;

101Smiley Tongueencil:57

102Smiley Tonguearker pen:24

103:hp studion shuffle n:10

104:samsung galexy ace:29

;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎07-22-2014 09:48 AM
Super User
Posts: 10,041

Re: seperate a single column values to each varibles

Posted in reply to KrishnaChandra

How about this :

data a;
input info $ 1-50;
cards;
101 pencil 57
102 parker pen 24
103 hp studion shuffle n 10
104 samsung galexy ace 29
;
run;
data b;
 set a;
 dep_id =scan(info,1,' ');
 dept_name=substr(info,findc(info,' ')+1,findc(trim(info),' ','b') - length(dep_id)-1 );
 dep_qty=substr(info,findc(trim(info),' ','b')+1);
run;

OR this one :

data a;
input info $ 1-50;
cards;
101 pencil 57
102 parker pen 24
103 hp studion shuffle n 10
104 samsung galexy ace 29
;
run;
data b;
 set a;
 dep_id =scan(compress(info,' ','kd'),1,' ');
 dept_name=compress(info,' ','ka');
 dep_qty=scan(compress(info,' ','kd'),-1,' ');
run;

Xia Keshan

Message was edited by: xia keshan

Trusted Advisor
Posts: 1,228

Re: seperate a single column values to each varibles

Posted in reply to KrishnaChandra

data a;

input prd_id name & $20. prod_qty;

cards;

101 pencil  57

102 parker pen  24

103 hp studion shuffle n  10

104 samsung galexy ace  29

;

run;

Frequent Contributor
Posts: 89

Re: seperate a single column values to each varibles

Posted in reply to KrishnaChandra

Thanks Xia Keshan. I got the op Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 245 views
  • 0 likes
  • 4 in conversation