arrays - Splitting a Delimited String Variable in SAS into Individual Variables -
i'm hoping can me out issue. trying take string variable contains series of delimited pick list responses. there can no response, 1 response (e.g., 1234), or several responses (e.g., 1234;9876) in single variable. different options delimited semicolon (;). i'd split single variable multiple variables based on delimiter.
for example, reasons=1234;9876 -> reason1=1234, reason2=9876
traditionally have done manual way using scan function. problem have (in case) 10 pick list items concatenated in single string.
data want; set got; length reason1-reason10 $10; if reasons ne ' ' reason1=scan(reasons, 1, ';'); if reasons ne ' ' reason2=scan(reasons, 2, ';'); if reasons ne ' ' reason3=scan(reasons, 3, ';'); ... run;
and on...
i feel array simplify process. however, learning how use arrays. advice on how split string variable more efficient code appreciated. thanks!
the best way solve varies based on size of dataset , complexity - how many possible variables might there be?
if there's small, known-ahead number of variables, method fine. arrays slightly:
data got; informat reasons $30.; input reasons $; datalines4; 123;456;789;101112 456;789;101112;131415 ;;;; run; data want; set got; length reason1-reason10 $10; array reason(10) $; _i = 1 dim(reason); reason[_i] = scan(reasons,_i,';'); end; run;
you don't have check if it's missing, scan behave fine that.
if you're not sure number of variables might (and don't want 'maximum' if never reached), proc transpose
better.
first put row out each scanned value, in 1 variable. transpose it.
adding id
variable make easier:
data got; informat reasons $30.; input id reasons $; datalines4; 1 123;456;789;101112 2 456;789;101112;131415 ;;;; run; data got_pret; set got; _i = 1 countw(reasons,';'); reason=scan(reasons,_i,';'); output; end; keep reason id; run; proc transpose data=got_pret out=want prefix=reason; id; var reason; run;
that has great advantage of not requiring know how many there ahead of time. 1 more step though, huge dataset may less preferred (though can use data step view
save of this).
Comments
Post a Comment