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.
I'm putting a second example with level = 7 :
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;
> 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 | . | . | . |
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 | . |
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 🙂
just klick on your name and you see everything you have posted, the list contains this topics clone.
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
> 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 | . | . | . |
Another case where a SWAP function would be nice to have.
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 ..
Thank you a lot and good morning to you !
Onizuka
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.