Hi there!
I have a problem using "Select when" statement. I have conditions, values and names in three arrays. I want to change my dataset (here are only sample) automaticaly although there are no errors or warnings I still don't get results. Nothing change in main dataset. I have information about "PROBLEM OBSERVATION" in my log but I don't know why. This is my code.
%let zm='APP_CHAR_BRANCH' 'APP_CHAR_BRANCH' 'APP_CHAR_BRANCH' 'APP_CHAR_CARS' 'APP_CHAR_CARS';
%let war='Empty' 'Fenitures, Radio-TV, Computers' 'DiY' 'Owner' 'No';
%let grp='3' '1' '2' '2' '1';
/* the "zm" and "war" variables are the exact text from observations from the main datasets ("zm" are variables names and "war" are the values in observations */
data vin_grp(drop=i);
set vin;
array zmienna (5) $15 zm1-zm5 (&zm.);
array warunek (5) $100 war1-war5 (&war.);
array grupa (5) $1 grp1-grp5 (&grp.);
do i = 1 to 5;
put warunek(i); /*I added this to see which observations has problems but It wasn't helpfull from me 😞 */
put zmienna(i);
put grupa(i);
select (zmienna(i));
when (warunek(i)) zmienna(i)=grupa(i); /* for example: when ('DiY') app_char_branch='2'; */
otherwise put 'PROBLEM OBSERVATION';
end;
end;
run;
Could you help me somehow?
M.M.
PS: Main data is in the attachments
I believe you are using SELECT incorrectly. The code you have is looking for observations that have:
zmienna(i) = warunek(i)
So these never match, and everything is a PROBLEM OBSERVATION.
What condition are you searching for in the SELECT statement? You don't have to code it ... just describe it.
I need to change all of the observations in vin dataset from all of the variable starting with "app_". I've done a categorization and want to change these text inside to number (group after cateforization :1,2 or 3).
I have table PODZIALY_ZNAKOWE where are variable name (zmienna), condition (war) and group (grp). On that base I want to change my datasets "vin". So for example I don't want to have anymore for variable app_char_branch observations like "Empty" but "2". Instead of "DiY" - "3" etc.
Can you try a format instead? It seems like it would be more intuitive and easier to follow. And it works better if you need to repeat this multiple times.
See the first example here:
http://www2.sas.com/proceedings/sugi30/001-30.pdf
But I have to work with that data later on using proc corr and do some analysis. So I guess format wan't be a good solution. And I have 200 more numeric variables to do the same with... 😞
That's exactly why you would use a format, it scales better than a whole lot of SELECT statements and is reusable. You still convert your data but it's much much easier. And you can create formats from data sets, so if you have a lookup table that maps a value and the corresponding new value you can easily create your formats without having to manually type each one out. No macro's and easier.
In a perfect world you'd have a lookup table structured like the following, which I suspect you may:
VariableName OldValue NewValue
If the "zm" are actually variable names, this would be the way to match them:
%let zm=APP_CHAR_BRANCH APP_CHAR_BRANCH APP_CHAR_BRANCH APP_CHAR_CARS APP_CHAR_CARS;
Then later:
array zmienna (5) &zm;
It's unusual to include the same variable multiple times within an array, but it should work in this case.
Instead of "array zmienna (5) $15 zm1-zm5 (&zm.);" ?
Nope it doesn't work.
PS: When I write a line like this:
when ('DiY') app_char_branch='2';
without using variables I got the results I needed, but of course I don't want to write these all conditions by myself...
Yes, that's the right thing to do. You might have to post the log so we can see the part that isn't working.
Take a look at the statement that I posted earlier:
%let zm= ......;
All the quotes should be removed. SAS uses quotes to indicate text, but removes the quotes when referring to variable names.
Still no. 😞 I've tried these. To add " ' " or no etc.
Note that @Astounding recommended:
array zmienna (5) &zm;
You used
array zmienna (5) $ (&zm.);
Parenthesized values in that location are not variable names but values to assign to the variables created
Oh! My bad. I changed the code several times so... My mistake! It's working! Thank you! 🙂
I have external question now :))
I want to create three variables automaticaly from that second dataset. Something like that.
PROC surveyselect data=PROJEKT.vin
out=PROJEKT.vin_sample
method=srs sampsize=3000; /* simple random sample - srs */
run;
proc sort data=PROJEKT.PODZIALY_ZNAKOWE out=PROJEKT.PODZIALY_ZNAKOWE;
by zmienna;
quit;
/* zmienna */
Proc sql noprint;
Select zmienna into :zm separated by ' '
from PROJEKT.PODZIALY_ZNAKOWE order by zmienna;
quit;
%put &zmienna.;
%let ilosc=&sqlobs;
/* warunek */
Proc sql noprint;
Select war into :war separated by ' '
from PROJEKT.PODZIALY_ZNAKOWE order by zmienna;
quit;
%put &war.;
/* grupa */
Proc sql noprint;
Select grp into :grp separated by ' '
from PROJEKT.PODZIALY_ZNAKOWE order by zmienna;
quit;
%put &grp.;
/* %let zm=APP_CHAR_BRANCH APP_CHAR_BRANCH APP_CHAR_BRANCH APP_CHAR_CARS APP_CHAR_CARS; */
/* */
/* %let war="when ('Empty')" "when ('Fenitures','Radio-TV','Computers')" "when ('DiY')" "when ('Owner')" "when ('No')"; */
/* %let war='Empty' 'Fenitures, Radio-TV, Computers' 'DiY' 'Owner' 'No'; */
/* */
/* %put &war.; */
/* %let grp='3' '1' '2' '2' '1'; */
/* %let grp=3 1 2 2 1; */
/* %put &grp.; */
data vin_grp(drop=i);
set vin_sample;
array zmienna (&sqlobs) &zm.;
array warunek (&sqlobs) $100 _temporary_ (&war.);
array grupa (&sqlobs) $1 _temporary_ (&grp.);
do i = 1 to &sqlobs;
select (zmienna(i));
when (warunek(i)) zmienna(i)=grupa(i);
otherwise put 'PROBLEM OBSERVATION';
end;
end;
run;
But then I will have new problems. I need to erase word "When" And "(" , ")" from condition (war) and also put more " ' " when there are more then one condition. Secondly in group (grp) the type of that column is numeric. And I will need character. Could you help me with that also?
I thougth that I use the condition variable (war) in a different way, easier, but I can't do that... I mean instead of
when (warunek(i)) zmienna(i)=grupa(i); (in this part I type variable warunek by hand so I overlooked the word "when" etc.)
do
warunek(i) zmienna(i)= grupa(i); (in this case I will take the whole text from variable but I get an error according to missing when word in select statement).
Advice: get this code to work without macro language at all. Once you have that done, you can consider how to apply macro language. There are too many features about the program that are either not working, or are working poorly.
Macro language does not add any functionality to a SAS program. It merely automates a program that is already working. But you have not gotten to the point of having a working program yet.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.