Thanks a lot in advance, friends. Below are samples of both what i have and what i want.
data have;
input category $25. ;
cards;
APPLEPLUS
A1
A2
3
4
BANANAPLUS
3
B5
6
PEARPLUS
2
Q15
C17
3
Z02
;
RUN;
DATA WANT;
input category $25. category1 $10. ;;
cards;
APPLEPLUS A1
APPLEPLUS A2
APPLEPLUS 3
APPLEPLUS 4
BANANAPLUS 3
BANANAPLUS B5
BANANAPLUS 6
PEARPLUS 2
PEARPLUS Q15
PEARPLUS C17
PEARPLUS 3
PEARPLUS Z02
;
RUN;
I tried using the following code, but it doesnt get me much. The values such as appleplus, BANANAPLUS etc are typically long all character but the others are much smaller and are either numerical or starting with an alphabet.
DATA WANT;
SET HAVE;
LENGTH CATEGORY1 $25.;
IF LENGTH(CATEGORY)> 7 THEN CATEGORY1 = CATEGORY;
RUN;
It's a one-step solution that skips creating HAVE and produces WANT from the original data.
I guess if you already have the data set HAVE, you could get WANT with:
data want;
set have (rename=(category=text));
if length(text) > 7 then category=text;
else do;
category1=text;
output;
end;
retain category;
drop text;
run;
Using these notions, you should be able to construct a data step to do the task:
You could also do analogous logic when reading in the raw data, i.e. straight from raw data to WANT, no need for HAVE.
One possibility:
data want;
input text $25. ;
length category $ 25 category1 $ 10;
if length(text) > 7 then category=text;
else do;
category1=text;
output;
end;
retain category;
cards;
APPLEPLUS
A1
A2
3
4
BANANAPLUS
3
B5
6
PEARPLUS
2
Q15
C17
3
Z02
;
Thanks Astounding, but not sure what you got here. I mean my want will give you the final output i want, so not sure what you mean by cards statement in your solution.
Also, the names such as appleplus and bananaplus are examples only, real names are 2-3 word characters. I have around 30,000 records.
It's a one-step solution that skips creating HAVE and produces WANT from the original data.
I guess if you already have the data set HAVE, you could get WANT with:
data want;
set have (rename=(category=text));
if length(text) > 7 then category=text;
else do;
category1=text;
output;
end;
retain category;
drop text;
run;
data have; input category $25. ; cards; APPLEPLUS A1 A2 3 4 BANANAPLUS 3 B5 6 PEARPLUS 2 Q15 C17 3 Z02 ; RUN; data want; set have; length cat $ 40; retain cat; if anydigit(category)=0 then do; cat=category;delete; end; run;
Thanks KSHARP. This one works as well, perhaps little better because there is no dependence on length.
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.
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.