Hello everyone!
I am a super newbie to SAS and would appreciate your help, in running the code below:
I want to filter data from the variable: preg_group to include only the miscarriage and normal data, and amongst these data, define new category variables for my BMI and age subsets.
But when i ran the code, i got this log:
What is the problem?
TIA everyone! 🙂
You can't use an IF command outside of a data step. Remove the RUN; after the DATA IMPORT line (because the RUN; statement ends the data step)
You can't use an IF command outside of a data step. Remove the RUN; after the DATA IMPORT line (because the RUN; statement ends the data step)
Please do not post code and logs as pictures, because it's unnecessary work for you and makes it harder for us to point out the mistakes.
Use the </> button for logs and other text data, and the "little running man" next to it for SAS code. Just copy/paste the text into the windows opened with these buttons.
You have a RUN statement immediately after the SET statement; this creates a step boundary and ends the DATA step. Therefore the next statements appear in "open code" (outside of a data step), where they are invalid. Remove this unnecessary RUN statement.
Improve your coding style by giving each statement its own line, and even more if a statement consists of several elements:
proc import
out=
datafile=
dbms=
replace
;
sheet = "........";
and so on. This improves the readability of your code, and makes it easier to debug and maintain. Also see Maxim 12.
Hi @duckypooh
Two things:
- you can use the IF/THEN statement outside a data step. That's why you get some errors in the log. TO avoid this, remove the "RUN;" just after the SET statement
- another thing is that you need to define a sufficient length fo your categorical variables. Otherwise, they will be set to the length of the first value and it will cause unexpected results (truncation)
Best,
data import;
set mcdata.import;
/*run; <---- remove run; */
if Preg_Group in ('Miscarriage', 'Normal');
length BMI_cat $10.; /* <---- set an appropriate length to avoid truncation */
if BMI > 28 then BMI_cat = 'BMI >28';
else if 19 <= BMI < 23 then BMI_cat = '19 <=BMI <23';
else BMI_cat = 'BMI Other';
length Age_cat $10.; /* <---- set an appropriate length to avoid truncation */
if Age <32 then Age_cat = 'Age <32';
else if Age >= 32 then Age_cat = 'Age >=32';
ln_Progestrone = log(Progestrone_nmol_L);
run;
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.