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

Hello ,

I am facing sorting issue in my data.So I need a solution for that.

the sample data is

data a;

infile datalines dlm=',';

input name $ 15.;

cards;

chilli 10g

chilli 100g

chilli 15g

chilli 200g

chilli 150g

chilli 1kg

chilli 250g

chilli 500g

;

run;

proc sort data=a;

by name;

run;

My required output is:

chilli 10g

chilli 15g

chilli 100g

chilli 150g

chilli 200g

chilli 250g

chilli 500g

chilli 1kg

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Assuming its just one word at the beginning you could change to:

proc sql;

  create table WANT (drop=WEIGHT) as

  select  NAME,

          case  when index(NAME,"kg")>0 then input(tranwrd(substr(NAME,1,index(NAME," ")),"kg",""),best.) * 1000

                else input(tranwrd(substr(NAME,1,index(NAME," ")),"g",""),best.) end as WEIGHT

  from    A

  order by WEIGHT;

quit;

You just need to modify the input part to manipulate the string to get only the number part, that depends on you data.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You need to split the numeric part out from the text, multiply it per the unit, then sort by that value:

proc sql;

  create table WANT (drop=WEIGHT) as

  select  NAME,

          case  when index(NAME,"kg")>0 then input(tranwrd(tranwrd(NAME,"chilli",""),"kg",""),best.) * 1000

                else input(tranwrd(tranwrd(NAME,"chilli",""),"g",""),best.) end as WEIGHT

  from    A

  order by WEIGHT;

quit;

avinesh
Fluorite | Level 6

HI RW9

In the sample data i mentioned only chilli But in real time data im having many products with different names for example

data a;

infile datalines dlm=',';

input name $ 15.;

cards;

chilli 10g

chilli 100g

chilli 15g

chilli 200g

chilli 150g

chilli 1kg

chilli 250g

chilli 500g

Tumeric 10g

Tumeric 100g

Tumeric 15g

Masala 200g

Masala 150g

Masala 1kg

Papad 250g

Papad 500g

;

run;

If i get a solution for this,it will be more helpful for me can you help me out thanks in advance

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Assuming its just one word at the beginning you could change to:

proc sql;

  create table WANT (drop=WEIGHT) as

  select  NAME,

          case  when index(NAME,"kg")>0 then input(tranwrd(substr(NAME,1,index(NAME," ")),"kg",""),best.) * 1000

                else input(tranwrd(substr(NAME,1,index(NAME," ")),"g",""),best.) end as WEIGHT

  from    A

  order by WEIGHT;

quit;

You just need to modify the input part to manipulate the string to get only the number part, that depends on you data.

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
  • 3 replies
  • 894 views
  • 3 likes
  • 2 in conversation