BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ffgsdf
Fluorite | Level 6

Hi, every body. 

 

I have wrote a SAS macro to import multiple data with dat format, but the variable and position have a little differnece. 

 

The data is separeted by each year and has three kind of format.

 

So, I created a macro variable according to the "year" and the input statement changed with different year.

 

The following code is my syntax, however, i found that something wrong. 

 

The data imported to my computer is correct one, but the input statement is all the ways according to the first one, which is between 1996 and 2003.

 

please correct my syntax and help me out.  

 

%macro a(star,myvar);

%do i = &star %to &myvar;

    %if  1996 <= &i <=2003  %then %do;

 filename total (

"F:\CD\R201_CD&i..DAT"

"F:\CD\R202_CD&i..DAT"

"F:\CD\R203_CD&i..DAT"

"F:\CD\R204_CD&i..DAT"

"F:\CD\R205_CD&i..DAT"

"F:\CD\R206_CD&i..DAT"

"F:\CD\R207_CD&i..DAT"

"F:\CD\R208_CD&i..DAT"

"F:\CD\R209_CD&i..DAT"

"F:\CD\R210_CD&i..DAT"

"F:\CD\R211_CD&i..DAT"

);

data CD&i;

infile total;

INPUT

FEE_YM $ 1-6 APPL_TYPE $ 7-7 HOSP_ID $ 8-41 APPL_DATE $ 42-49 CASE_TYPE $ 50-51 SEQ_NO  52-57 CURE_ITEM_NO1 $ 58-59 CURE_ITEM_NO2 $ 60-61 CURE_ITEM_NO3 $ 62-63 CURE_ITEM_NO4 $ 64-65 FUNC_TYPE $ 66-67 FUNC_DATE $ 68-75 TREAT_END_DAT $ 76-83 ID_BIRTHDAY $ 84-91 ID $ 92-123 CARD_SEQ_NO $ 124-125 GAVE_KIND $ 127-127 PART_NO $ 128-130 ACODE_ICD9_1 $ 131-135 ACODE_ICD9_2 $ 136-140 ACODE_ICD9_3 $ 141-145 ICD_OP_CODE $ 146-149 DRUG_DAY  150-151 MED_TYPE $ 152-152 PRSN_ID $ 153-184 PHAR_ID $ 185-216 DRUG_AMT  217-224 TREAT_AMT  225-232 TREAT_CODE $ 233-244 DIAG_AMT  245-252 DSVC_NO $ 253-264 DSVC_AMT  265-272 BY_PASS_CODE $ 273-274 T_AMT  275-282 PART_AMT  283-290 T_APPL_AMT  291-298 ID_SEX $ 299-299 ;

RUN;

%End;

%else %if  2004 <= &i <=2011  %then %do;

filename total (

"F:\CD\R201_CD&i..DAT"

"F:\CD\R202_CD&i..DAT"

"F:\CD\R203_CD&i..DAT"

"F:\CD\R204_CD&i..DAT"

"F:\CD\R205_CD&i..DAT"

"F:\CD\R206_CD&i..DAT"

"F:\CD\R207_CD&i..DAT"

"F:\CD\R208_CD&i..DAT"

"F:\CD\R209_CD&i..DAT"

"F:\CD\R210_CD&i..DAT"

"F:\CD\R211_CD&i..DAT"

);

data CD&i;

infile total;

INPUT

FEE_YM $ 1-6 APPL_TYPE $ 7-7 HOSP_ID $ 8-41 APPL_DATE $ 42-49 CASE_TYPE $ 50-51 SEQ_NO  52-57 CURE_ITEM_NO1 $ 58-59 CURE_ITEM_NO2 $ 60-61 CURE_ITEM_NO3 $ 62-63 CURE_ITEM_NO4 $ 64-65 FUNC_TYPE $ 66-67 FUNC_DATE $ 68-75 TREAT_END_DATE $ 76-83 ID_BIRTHDAY $ 84-91 ID $ 92-123 CARD_SEQ_NO $ 124-127 GAVE_KIND $ 128-128 PART_NO $ 129-131 ACODE_ICD9_1 $ 132-136 ACODE_ICD9_2 $ 137-141 ACODE_ICD9_3 $ 142-146 ICD_OP_CODE $ 147-150 DRUG_DAY  151-152 MED_TYPE $ 153-153 PRSN_ID $ 154-185 PHAR_ID $ 186-217 DRUG_AMT  218-225 TREAT_AMT  226-233 TREAT_CODE $ 234-245 DIAG_AMT  246-253 DSVC_NO $ 254-265 DSVC_AMT  266-273 BY_PASS_CODE $ 274-275 T_AMT  276-283 PART_AMT  284-291 T_APPL_AMT  292-299 ID_SEX $ 300-300; 

RUN;

%End;

%else %if  2012 <= &i %then %do;

 filename total (

"F:\CD\R201_CD&i..DAT"

"F:\CD\R202_CD&i..DAT"

"F:\CD\R203_CD&i..DAT"

"F:\CD\R204_CD&i..DAT"

"F:\CD\R205_CD&i..DAT"

"F:\CD\R206_CD&i..DAT"

"F:\CD\R207_CD&i..DAT"

"F:\CD\R208_CD&i..DAT"

"F:\CD\R209_CD&i..DAT"

"F:\CD\R210_CD&i..DAT"

"F:\CD\R211_CD&i..DAT"

);

data CD&i;

infile total;

INPUT

FEE_YM $ 1-6 APPL_TYPE $ 7-7 HOSP_ID $ 8-41 APPL_DATE $ 42-49 CASE_TYPE $ 50-51 SEQ_NO  52-57 CURE_ITEM_NO1 $ 58-59 CURE_ITEM_NO2 $ 60-61 CURE_ITEM_NO3 $ 62-63 CURE_ITEM_NO4 $ 64-65 FUNC_TYPE $ 66-67 FUNC_DATE $ 68-75 TREAT_END_DATE $ 76-83 ID_BIRTHDAY $ 84-91 ID $ 92-123 CARD_SEQ_NO $ 124-127 GAVE_KIND $ 128-128 PART_NO $ 129-131 ACODE_ICD9_1 $ 132-146 ACODE_ICD9_2 $ 147-161 ACODE_ICD9_3 $ 162-176 ICD_OP_CODE $ 177-191 DRUG_DAY  192-193 MED_TYPE $ 194-194 PRSN_ID $ 195-226 PHAR_ID $ 227-258 DRUG_AMT  259-266 TREAT_AMT  267-274 TREAT_CODE $ 275-286 DIAG_AMT  287-294 DSVC_NO $ 295-306 DSVC_AMT  307-314 CASE_PAY_CODE $ 315-316 T_AMT  317-324 PART_AMT  325-332 T_APPL_AMT  333-340 ID_SEX $ 341-341 TRAN_IN_HOSP_ID $ 342-375 PAT_TRAN_OUT $ 376-376 APPL_CAUSE_MARK $ 377-377  ;

RUN;

%end;

%End;

 %mend a;

%a(2011,2012);

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

Using MLOGIC is key when debugging macros:

 

MLOGIC(A):  %DO loop beginning; index variable I; start value is 2011; stop value is 2012; by
      value is 1.
MLOGIC(A):  %IF condition 1996 <= &i <=2003 is TRUE

 

Unfortunately, %if doesn't work the exact way as normal if.

Changing to:

%if  1996 <= &i and &i <=2003  %then %do;

will probably fix your problem.

 

Another tip: you can simplyfy your code by just doing the %if around the input statement . The other code parts looks exactly the same (I could have missed something 🙂 )

Data never sleeps

View solution in original post

1 REPLY 1
LinusH
Tourmaline | Level 20

Using MLOGIC is key when debugging macros:

 

MLOGIC(A):  %DO loop beginning; index variable I; start value is 2011; stop value is 2012; by
      value is 1.
MLOGIC(A):  %IF condition 1996 <= &i <=2003 is TRUE

 

Unfortunately, %if doesn't work the exact way as normal if.

Changing to:

%if  1996 <= &i and &i <=2003  %then %do;

will probably fix your problem.

 

Another tip: you can simplyfy your code by just doing the %if around the input statement . The other code parts looks exactly the same (I could have missed something 🙂 )

Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 711 views
  • 0 likes
  • 2 in conversation