SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
VISHNU239
Obsidian | Level 7

data m;
input info $ 1 - 50;
cards;
101 pencils 39
102 parker pens 21
103 apple ipod touch & shuffle 09
104 dell studio laptop 03
run;

 

 

/*get the starting nos as pr_id, ending nos as pr_qty and the rest as pr_name*/

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

One way 

 

data want;
	set m;
	pr_id=scan(info, 1);
	pr_qty=scan(info, -1);
	pr_name=substr(info, findw(info, scan(info, 2)), findw(info, scan(info, -1))-findw(info, scan(info, 2)));
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I like this offering, one thing I would add is once you have pr_id and pr_qty, you could drop that from the first variable and avoid the longer code:

data want;
  set m;
  pr_id=scan(info, 1);
  pr_qty=scan(info, -1);  pr_name=tranwrd(tranwrd(info,pr_id,""),pr_qty,"");
run;  

Just an option. 

VISHNU239
Obsidian | Level 7

i couldn't see any data in output. All i can see is table of pr_id,pr_qty,Info,pr_name.

s_lassen
Meteorite | Level 14

Another possibility is to use PRX functions, which enables you to check for numbers in the right places:

data want;
  set m;
  length pr_id $5 pr_name $40 pr_qty $5;
  prxid=prxparse('/^(\d+) (.+) (\d+)\s*$/');
  if not prxmatch(prxid,info) then
    error 'No match';
  else do;
    pr_id=prxposn(prxid,1,info);
    pr_name=prxposn(prxid,2,info);
    pr_qty=prxposn(prxid,3,info);
    end;
  keep info pr_:;
run;

The PRX string searches for beginning of string "^", some digits "\d+" which are placed in the first capture buffer (the "()" around), a blank, a string which can be anything (".+", in the second capture buffer), a blank, some digits in the third capture buffer, and finally some whithespace "\s*" and end of string "$". You then use PRXPOSN to get at the capture buffers.  

VISHNU239
Obsidian | Level 7
i am sorry i forgot to run the initial data m. even after running it i got all of them under info. but i want them seperately 101 102... under pr_id.pencils, parker.... under pr_name and the rest under pr_qty.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Show and example of what you mean.

VISHNU239
Obsidian | Level 7
 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2755 views
  • 3 likes
  • 4 in conversation