BookmarkSubscribeRSS Feed
kbos
Calcite | Level 5

Hi all!

 

I am new to SAS EG so I am looking for some quick help. (Shout out to everyone for all the help I've been able to get from reading past posts!!)

 

I have a field that I have parsed out into 3 columns using the scan function:

 

Orig_ValueNew1New2New3
"M=480","N=PTO","T=N""M=480""N=PTO""T=N"

 

That works simple enough, but there are a few Orig_Value fields that have the same data, but in different order:

 

Orig_ValueNew1New2New3
"N=PTO","T=N","M=480""N=PTO""T=N""M=480"

 

How do I get the values to line up in the same columns? So for example all the M=480s would be in New1, all the PTOs would be in New2...etc

6 REPLIES 6
rogerjdeangelis
Barite | Level 11
This probably will not help but..

If you do some cleanup you can use named input;

data want;
 input n=$ t=$ m=;
cards4;
N=PTO T=N M=480
N=ROG T=Y M=280
N=SAL T=N M=380
;;;;
run;quit;

Up to 40 obs WORK.WANT total obs=3

Obs     N     T     M

 1     PTO    N    480
 2     ROG    Y    280
 3     SAL    N    380

Astounding
PROC Star

This comes close to what you are asking for.  It removes your original variable, and assigns different names to the final variables.  But it does split them while maintaining the order that you want.

 

data temp;

set have;

record_num = _n_;

if orig_value > ' ';

do i=1 to 5;

value = scan(orig_value, i, ',');

if value > ' ' then output;

end;

run;

 

proc transpose data=temp out=want (drop=_name_);

var value;

id value;

by record_num;

run;

 

This example assumes you have a maximum of five values on any one observation.  If there could be more, the DO loop would have to expand (such as 1 to 10 instead of 1 to 5).

art297
Opal | Level 21

Here is one way:

 

data want;
  informat orig_value $50.;
  input orig_value &;
  array want(3)$ New1-New3;
  do i=1 to 3;
    if substr(scan(orig_value,i,','),1,3)='"M=' then New1=scan(orig_value,i,',');
    else if substr(scan(orig_value,i,','),1,3)='"N=' then New2=scan(orig_value,i,',');
    else if substr(scan(orig_value,i,','),1,3)='"T=' then New3=scan(orig_value,i,',');
  end;
  cards;
"M=480","N=PTO","T=N"
"N=PTO","T=N","M=480"
"N=PTO","T=N"
;

HTH,

Art, CEO, AnalystFinder.com

 

Ksharp
Super User

call sortc()

 

data want;
infile cards dlm=',';
  input v1 $   v2 $ v3 $;
call sortc(of v1-v3);
  cards;
"M=480","N=PTO","T=N"
"N=PTO","T=N","M=480"
"N=PTO","T=N",.
;
run;
art297
Opal | Level 21

@Ksharp: sortc won't always produce the desired result. Here is one case where it will fail:

 

data want;
  infile cards dlm=',';
  input v1 $   v2 $ v3 $;
  call sortc(of v1-v3);
  cards;
"M=480","N=PTO","T=N"
"N=PTO","T=N","M=480"
"N=PTO","T=N",.
"M=PTO","T=N",.
;
run;

Art

kbos
Calcite | Level 5

Thanks everyone for your help! I received a lot of great ideas so I ended up taking a piece from each and came up with this:

 

Select (substr(scan(ovr_new_value,1,','),1,8));

when ('"WRKP_MI') Minutes = scan(ovr_new_value, 1, ',');

when ('"WRKP_RE') TC = scan(ovr_new_value, 1, ',');

when ('"WRKP_TC') TYPE = scan(ovr_new_value, 1, ',');

end;

 

Then repeated the above for the New2 and New3 columns. Works like a charm and does exactly what I want it to do.

 

Again, thanks!  You guys are awesome!!!

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