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

Hello,

 

I want to reorder lot of variables but I can't reorder them using the ' : '

 

Like :

 

data have ;
format a_1 a_2 a_3 b_1 b_2 d c $2. ;
input a_1 a_2 a_3 b_1 b_2 d c ;
cards;
a1 a2 a3 b1 b2 d c
a1 a2 a3 b1 b2 d c
;

Data want ;
retain d b: a: ;
set have ;
run ;

Is there a "trick" to do this on a retain without using a1-a3 b1-b2 ? Because on my real datas, there is no 1,2,3,4, ....

 

Thanks,

 

Mateo

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. There may be a smarter way, but this works. 

 

data have ;
format a_1 a_2 a_3 b_1 b_2 d c $2. ;
input a_1 a_2 a_3 b_1 b_2 d c ;
cards;
a1 a2 a3 b1 b2 d c
a1 a2 a3 b1 b2 d c
;

proc sql noprint;
    select name into :bvars separated by ' '
    from dictionary.columns
    where libname='WORK' & memname='HAVE' & name like 'b%';
    select name into :avars separated by ' '
    from dictionary.columns
    where libname='WORK' & memname='HAVE' & name like 'a%';
quit;

%put &bvars.;
%put &avars.;

Data want ;
retain d &bvars. &avars. ;
set have ;
run ;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

The reason why this doesn't work is that when the data step hits the Retain Statement during compilation, it does not yet know what variables to look for with the colon operator because it has not yet reached the Set Statement. 

 

I think you have to read the relevant variable names through metadata to achieve this. However, it may be more convenient just typing out the variable names. So if you are still interested, I will provide the code.

Onizuka
Pyrite | Level 9

Oh yes... true... Thank you @PeterClemmensen .. !

 

I have a lot of variables, so yes, i can rename them "manually" but i would take a looong time !

I think that I have ~ 190 variables something like that ahaha

 

There are like :

a_q1_i1_nbrep

a_q1_i1_rests

a_q1_i1_value

...

a_q1_i2_nbrep

....

b_q1_.....

....

 

So if you have a piece of code, i would be very happy haha

 

Thanks again,

 

Mateo

PeterClemmensen
Tourmaline | Level 20

Ok. There may be a smarter way, but this works. 

 

data have ;
format a_1 a_2 a_3 b_1 b_2 d c $2. ;
input a_1 a_2 a_3 b_1 b_2 d c ;
cards;
a1 a2 a3 b1 b2 d c
a1 a2 a3 b1 b2 d c
;

proc sql noprint;
    select name into :bvars separated by ' '
    from dictionary.columns
    where libname='WORK' & memname='HAVE' & name like 'b%';
    select name into :avars separated by ' '
    from dictionary.columns
    where libname='WORK' & memname='HAVE' & name like 'a%';
quit;

%put &bvars.;
%put &avars.;

Data want ;
retain d &bvars. &avars. ;
set have ;
run ;
Onizuka
Pyrite | Level 9

Thanks to your idea, I was currently trying to do the same thing by going through the dictionary.columns but you have been much faster than me! haha

 

I try this and I come back after 😃

 

In any case, thank you very much!

PeterClemmensen
Tourmaline | Level 20

No problem, let me know if it works for you 🙂

Onizuka
Pyrite | Level 9

It seems to work ! Thank you for teaching me this 🙂

 

Mateo

FreelanceReinh
Jade | Level 19

Hello @Onizuka,

 

An alternative approach uses a "non-executable" SET statement in conjunction with selected KEEP= dataset options (where you can use variable lists):

data want;
retain d;
if 0 then set have(keep=b:) have(keep=a:);
set have;
run;
Onizuka
Pyrite | Level 9

@FreelanceReinh wrote:

Hello @Onizuka,

 

An alternative approach uses a "non-executable" SET statement in conjunction with selected KEEP= dataset options (where you can use variable lists):

data want;
retain d;
if 0 then set have(keep=b:) have(keep=a:);
set have;
run;

 

Hello @FreelanceReinh , thank you for your answer, I will try your solution too, thanks again ! 🙂

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
  • 8 replies
  • 1822 views
  • 6 likes
  • 3 in conversation