BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
devsas
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

Using these notions, you should be able to construct a data step to do the task:

 

  1. Rename category to category1 when reading HAVE, so now category is a new variable and category1 is old.
  2. Instead of looking for a long word to determine the presence of a new category, why not look for the string 'PLUS', as in
       if find(category1,'PLUS') then category=category1;
  3. output only if #2 is not true (because it will have category, but not category1).
  4. Retain the "new" variable category, to carray foward to the subsequent category1 series.

 

You could also do analogous logic when reading in the raw data, i.e. straight from raw data to WANT, no need for HAVE.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

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
;

 

devsas
Pyrite | Level 9

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.

Astounding
PROC Star

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;

Ksharp
Super User

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;

devsas
Pyrite | Level 9

Thanks KSHARP. This one works as well, perhaps little better because there is no dependence on length.

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
  • 1153 views
  • 2 likes
  • 4 in conversation