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_Value | New1 | New2 | New3 |
"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_Value | New1 | New2 | New3 |
"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
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
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).
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
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;
@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
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.