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'm SO SORRY, i don't know what just happen but i delete my previous topic by mistake ... !!!!! I'm writting it again, so sorry for people who answer me... @gamotte  @BruceBrad ..

 

---------------------------------------------------------------------------------------

I have 9 variables :

 

- Level (which value is between 1 and 😎

- cniv1

- cniv2

- ...

- cniv8

 

All cniv variables contain a code.

 

I have to change the value of cniv variables depend of the level of hierarchy

 

I'm putting an example to explicate what i want with a level = 5

 

The first board is what i have and the second one is what i want.

 

Capture.PNG

 

I'm putting a second example with level = 7 :

Capture.PNG

 

I don't know how explicate it with sentences, i hop you understood my problem and my bad english...

 

Good afternoon,

 

Onizuka

 

-------------------------------------

I'm putting there the answers gave me gamotte and BruceBradd but it doesn't work because it sort the value descending and not by order of hierarchy (this is not clear i know..)

 

/* Gamotte's answer */
data test; set cim_hierarchie; array cniv(8) cniv8-cniv1; call sortn(of cniv(*)); run; /* BruceBradd's answer */ data test; set cim_hierarchie; array cniv(*) cniv1-cniv8; array newcniv(*) newcniv1-newcniv8; do i = 1 to 8; newcniv(i)=cniv(i); end; do j = 1 to 8; cniv(j)=largest(j, of newcniv1-newcniv8); end; drop new: i j; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

> Your solution doesn't answer to my problem because all values (characters values) have to be invert !

Oh I see.

Sorry I only looked at the values in red.

This then:

data HAVE;
  input LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8 ;
cards;
5 5000 30 600  500  5 . . .
7 5000 30 600  500  5 30 4400 .
6 1 2 3 4 5 6 . .
5 1 2 3 4 5 . . .
run;

data WANT;
  set HAVE;
  array CNIV (*) CNIV1-CNIV8;
  do I=1 to int(LEVEL/2);
    TMP             = CNIV[I];
    CNIV[I]         = CNIV[LEVEL-I+1];
    CNIV[LEVEL-I+1] = TMP;
  end;
  drop TMP I;
run;
LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8
5 5 500 600 30 5000 . . .
7 4400 30 5 500 600 30 5000 .
6 6 5 4 3 2 1 . .
5 5 4 3 2 1 . . .

 

 

 

View solution in original post

8 REPLIES 8
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
  input LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8 ;
cards;
5 5000 30 600  500  5 . . .
7 5000 30 600  500  5 30 4400 .
run;

data WANT;
  set HAVE;
  array CNIV(*) CNIV1-CNIV8;
  TMP        = CNIV1;
  CNIV1      = CNIV[LEVEL];
  CNIV[LEVEL]= TMP;
  drop TMP;
run;
LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8
5 5 30 600 500 5000 . . .
7 4400 30 600 500 5 30 5000 .
Onizuka
Pyrite | Level 9

Hello ChrisNZ, thank you for the answer, there is another topic (same name) that i have created. I don't even know how do you see this topic because i completly lost the track of it (i thought it has been deleted !).

 

Your solution doesn't answer to my problem because all values (characters values) have to be invert !

 

So when you have this table you have created :

 

data HAVE;
  input LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8 ;
cards;
5 5000 30 600  500  5 . . .
7 5000 30 600  500  5 30 4400 .
run;

the results should be :

 

5     5     500     600     30     5000     .     .     .

7     4400     30     5     500     600     30     5000     .

 

Thanks anyway for trying !

 

Onizuka 🙂

andreas_lds
Jade | Level 19

just klick on your name and you see everything you have posted, the list contains this topics clone.

Onizuka
Pyrite | Level 9

Ok, thank you, but i think there was a weird bug because when I try to edit my post it just leave the page and then when i came back to "Home / SAS Programming / Programming" my topic wasn't here anymore .. I should have tried to see my profile.

 

Good afternoon and sorry for the inconvenience.

 

Onizuka

ChrisNZ
Tourmaline | Level 20

> Your solution doesn't answer to my problem because all values (characters values) have to be invert !

Oh I see.

Sorry I only looked at the values in red.

This then:

data HAVE;
  input LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8 ;
cards;
5 5000 30 600  500  5 . . .
7 5000 30 600  500  5 30 4400 .
6 1 2 3 4 5 6 . .
5 1 2 3 4 5 . . .
run;

data WANT;
  set HAVE;
  array CNIV (*) CNIV1-CNIV8;
  do I=1 to int(LEVEL/2);
    TMP             = CNIV[I];
    CNIV[I]         = CNIV[LEVEL-I+1];
    CNIV[LEVEL-I+1] = TMP;
  end;
  drop TMP I;
run;
LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8
5 5 500 600 30 5000 . . .
7 4400 30 5 500 600 30 5000 .
6 6 5 4 3 2 1 . .
5 5 4 3 2 1 . . .

 

 

 

ChrisNZ
Tourmaline | Level 20

Another case where a SWAP function would be nice to have.

 

Onizuka
Pyrite | Level 9

Hello Chris, no problem haha thank you !

 

You could not know because I have two topics open on the same subject (not very practical I know) but I complicated the problem (not for pleasure) ... !!

 

 

Imagine I have :

data HAVE;
  input LEVEL CNIV1 CNIV2 CNIV3 CNIV4 CNIV5 CNIV6 CNIV7 CNIV8 ;
cards;
5 5000 30 600  500  5 . . .
7 5000 30 600  500  5 30 4400 .
5 1 . 3 . 5 . . .  /* 5 levels but 2 missing values */
run;

And if I want to keep the missing values like you can see on this output just below. You can see for the last line, it inverse the values but keeping the missing values.. This is to deal with the case where a society that has 5 hierarchy level transmits the data with 2 missing levels (it's an example of course).. I don't know if you have any ideas ..

 

Capture.PNG

Thank you a lot and good morning to you !

 

Onizuka

Onizuka
Pyrite | Level 9

Chris,

 

It seems that Mister Kurt found the solution, i'm going to test it adding some missing values. Thank to you for your time and you work 🙂

 

I put here the link to the other subject.

 

https://communities.sas.com/t5/SAS-Programming/change-variable-names-not-so-easy/td-p/549898/highlig...

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
  • 812 views
  • 0 likes
  • 3 in conversation