BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jonatan_velarde
Lapis Lazuli | Level 10

Good day:


I have this data set:


data have;

input Order ID;

cards;

1 ROBERT

2 CARLOS

3 EDWARD

4 JHON

;


now i need this:


ORDER ID STATUS

1 ROBERT ok

1.1 ROBERT ok

1.2 ROBERT ok

1.3 ROBERT ok

2 CARLOS ok

2.1 CARLOS ok

2.2 CARLOS ok

2.3 CARLOS ok

3 EDWARD ok

3.1 EDWARD ok

3.2 EDWARD ok

3.3 EDWARD ok

4 JHON ok

4.1 JHON ok

4.2 JHON ok

4.3 JHON ok

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi @jonatan_velarde,

 

Another alternative:

 

data have;
   input Order ID $8.;
   cards;
1 ROBERT
2 CARLOS
3 EDWARD
4 JHON
;


data want;
   set have;

   retain status 'OK';

   do order = order to (order + 0.3) by 0.1;
      output;
   end;
run;

 

 

Kind regards,

Amir.

View solution in original post

6 REPLIES 6
Ksharp
Super User
data have;
input Order ID $;
cards;
1 ROBERT
2 CARLOS
3 EDWARD
4 JHON
;

data want;
 set have;
 status='ok';
output;
 do i=1 to 3;
  order=order+0.1;output;
 end;
 drop i;
run;
PeterClemmensen
Tourmaline | Level 20
data have;
input Order ID $;
cards;
1 ROBERT 
2 CARLOS 
3 EDWARD 
4 JHON   
;

data want(drop = i o);
   set have(rename = Order = o);
   status = 'ok';
   order = put(o, 8. -l);
   output;
   do i = 1 to 3;
      order = catx('.', cats(o), i);
	  output;
   end;
run;
Amir
PROC Star

Hi @jonatan_velarde,

 

How about:

 

data have;
   input Order ID $8.;
   cards;
1 ROBERT
2 CARLOS
3 EDWARD
4 JHON
;


data want(drop = i);
   set have;

   retain status 'OK';

   do i = 1 to 4;
      output;
      order = order + 0.1;
   end;
run;

 

 

Kind regards,

Amir.

Amir
PROC Star

Hi @jonatan_velarde,

 

Another alternative:

 

data have;
   input Order ID $8.;
   cards;
1 ROBERT
2 CARLOS
3 EDWARD
4 JHON
;


data want;
   set have;

   retain status 'OK';

   do order = order to (order + 0.3) by 0.1;
      output;
   end;
run;

 

 

Kind regards,

Amir.

FreelanceReinh
Jade | Level 19

Hello @jonatan_velarde,

 

I would consider storing the two levels of ORDER (integer and decimal part in your description) in separate variables if possible.

data test;
retain order1 order2 id;
retain status 'ok';
set have(rename=(order=order1));
do order2=., 1 to 3;
  output;
end;
run;

 

Thus you would avoid these potential issues:

  • Values like "m.n" (with integers m and n) in a character variable (as suggested by @PeterClemmensen) would require precautions such as inserting leading blanks or zeros for the case that some of the m or n get greater than 9. Otherwise, the default alphabetic sort order might not be satisfactory.
  • Numeric values m.n are prone to numeric representation issues:
    607   data _null_;
    608   if 2.2+0.1 ne 2.3 then put 'Surprised?';
    609   run;
    
    Surprised?
    NOTE: DATA statement used (Total process time):
    So you would need to modify the other code suggestions (e.g., apply the ROUND function) to avoid unexpected results. Plus the issue if n>9.
ghosh
Barite | Level 11

Another approach, using Cartesian product, that also gives you the flexibility of using letters, example, in the order variable.

data have;
input Order ID $;
cards;
1 ROBERT 
2 CARLOS 
3 EDWARD 
4 JHON   
;
data seq;
  input n;
cards;
0.0
0.1
0.2
0.3
;  
proc sql;
   create table Cart as
   select have.*,seq.*
   from have, seq;
quit;
data want;
   set Cart;
      retain status 'ok';
     Order=order+n;
     drop n;
 run;
proc sort;
  by Order ID;
run;
proc print;
run;

ghosh_0-1646235029834.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 688 views
  • 1 like
  • 6 in conversation