This is still several steps away from being finished. Here's the first change, modifying this step:
Oh sorry. I missed that spot! What's now?
Next: Any place the number 5 is being used, shouldn't that be replaced with &SQLOBS?
After that: Accept the fact that requiring the use of SELECT adds unnecessarily to the complexity of the program. Decide if you have to keep it rather than eliminate it.
Yes, you're right. I change the code to check something and I forgot to change it again after while. Now it's "&sqlobs" there.
Unfortunately I have to use 'select when' statement, so I can't throw that away 😞
EDIT: But If you have time and you want to you can also show me a different method - 'less heavy' for the processor. But on my classes I have to use 'select when'
Some replacement logic for the final DO loop, omitting SELECT:
do i=1 to &ilosc.;
if zmienna{i} = warunek{i} then zmienna{i} = grupa{i};
end;
do i=1 to &ilosc.;
if zmienna{i} not in ('1' '2' '3') then put 'PROBLEM OBSERVATION: element ' i;
end;
You can put the SELECT logic inside the DO loop, but it complicates the code, and might print PROBLEM OBSERVATION extra times.
All of those comments are subject to testing, which I leave to you.
Yes it's working. Thanks. But I still don't understand why if I using select statement there are some problems with array called zmienna(i) ? Now everything is allright. Hmn.
PS; Maybe it will be a stupid question but is there different statement using "when" than "select when" statement? My teacher said we should used "when" ... I thought he meant 'select when'. But you said that it's way more complicated and now I wounder....
WHEN can also be used in PROC SQL, in a CASE clause. But that is very different.
SELECT has two forms:
SELECT;
SELECT (value);
Either form uses WHEN statements to find a match. The first one has to add a comparison such as WHEN (a=b) to find a match. The second form does not use a comparison: WHEN (a) to see whether A is equal to VALUE.
Also note, in my previous post there is no good reason to use two DO loops. Both statements could be included in the same DO loop.
Thank you for all your help. Now I will try to modify this code for numeric dataset and conditions :]
My numeric dataset is bit of a challenge. I thought I'll just change a little bit your code but conditions are much different that in character dataset. Could you give last look on that?
I can't divide the condition values cause it's the whole statement that I need. If I use the extra apostrophes then nothing happens.
My code:
proc sort data=PROJEKT.PODZIALY_NUMERYCZNE out=PROJEKT.PODZIALY_NUMERYCZNE;
by zmienna grp;
quit;
/* zmienna */
Proc sql noprint;
Select zmienna into :zm2 separated by ' '
from PROJEKT.PODZIALY_NUMERYCZNE;
quit;
%put &zm2.;
/* warunek */
Proc sql noprint;
Select cats("'",war,"'")
into :war2 separated by ' '
from PROJEKT.PODZIALY_NUMERYCZNE;
quit;
%put &war2.;
/* grupa */
Proc sql noprint;
Select cats("'",grp,"'")
into :grp2 separated by ' '
from PROJEKT.PODZIALY_NUMERYCZNE;
quit;
%put &grp2.;
data vin_grp2(drop=i);
set PROJEKT.vin2_sample;
array warunek (&ilosc2) $100 _temporary_ (&war2.);
array grupa (&ilosc2) $1 _temporary_ (&grp2.);
array zmienna (&ilosc2) &zm2.;
put warunek{1};
put warunek{3};
do i = 1 to &sqlobs;
warunek{i}=tranwrd(warunek{i}, "'", "");
if warunek{i} then zmienna{i}=grupa{i};
end;
run;
Information from the log:
Rant: Are you 100% positive you need to use this approach? I know you said it was a class requirement earlier but it's a really bad way to design code and you're creating a monolith that will be hard to modify and debug in the future. Using Formats or lookup tables is a lot cleaner in the long run. If you plan to ever modify or use this program again, I would strongly recommend a different appraoch.
End Rant.
Somethings to keep in mind:
1. Your arrays are currently declared as characters arrays and arrays cannot hold mixed types, so you need one for numeric variables and one for character variables.
array warunek (&ilosc2) $100 _temporary_ (&war2.);
array grupa (&ilosc2) $1 _temporary_ (&grp2.);
2. Rather than CATS with the quotation marks use the QUOTE() function, it can do either single or double quotes, check the docs if you need to use single quotes.
Select cats("'",grp,"'")
3. You don't need multiple queries to create the two macro variable lists, you can do that in a single PROC SQL query.
4. Your code here isn't using Select WHEN so it seems like this doesn't align with that methodology either, so see my Rant above.
5. This (&ilosc2) isn't defined in the code you've posted, it probably needs to be calculated as well, for each recoding process.
6. Comment your code so you understand what each step is doing, and we understand what you're intending each step to do. Otherwise my assumption is the code posted is correct and does what you want.
I changed my code and forgot about 'when' statement because of running out of time. I thought I will give the results to my colleague. Because she needs to work on that to finished this project. And in meanwhile I will rethink this part of the code. Because I should use select when statement or I don't know something connected with "when"... Oh my! This project is killing me... I had only 5 classes about SAS - really basic stuffs. I write my code using information from the net, blogs etc. I don't have any experience working with SAS. So...
Forget about my code if you think it's terrible and let's start from the beginning. I have two datasets: one main dataset (vin2) and second one with conditions (podzialy_numeryczne). I want to change my main dataset using the conditions from the second dataset. How can I do that in the most simple way?
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.