|
You cannot adjust the length dynamically, without using some
extra-datastep functionality. Basically, you'd want to look into a
PROC CONTENTS output, or dictionary.columns, and identify variables
that matched your criteria (length < 6 and in your list of variables
to adjust), and build a list of length statements; then run those
length statements _before_ the set statement (otherwise SET will
predefine the lengths for you, and you won't be able to adjust the
length).
Personally I'd rather have this part of my program hardcoded to a
length if possible - but of course for your purposes that may not be
possible.
The code would be something around this, not tested or particularly careful:
select cats("%lengthmod(",name,')') into :lenlist separated by ' '
from dictionary.columns where libname='WORK' and memname='DATASETNAME'
and name like 'var%' and length < 6;
%macro lengthmod(varname);
length &varname. 6;
%mend lengthmod;
data whatever;
&lenlist.
set datasetname;
...
run;
-Joe
On Fri, Nov 18, 2011 at 5:38 PM, b miner <b_miner@live.com> wrote:
> One follow-up I have from this. I got my code to run great now! but when I go to use the code dynamically which ends up looking like this:
>
> data inputTable;
> set inputTable;
> if var1 eq '' then var1='_MISS_';
> if var2 eq '' then var2='_MISS_';
> ..
> if varp eq '' then varp='_MISS_';
> run;
>
> When the length of one of the character var is less than _MISS_ (6), the replacement value is truncated. Is there a way to increase the length for only those fields that are less than a pre-set value and where this recoding is taking place?
> I'm thinking right in line with the various if statements?
>
>
> Thanks!
>
> Brian
>
> From: b_miner@live.com
> To: ybolotin@schools.nyc.gov; sas-l@listserv.uga.edu
> Subject: RE: Marco Variable Building of Code
> Date: Fri, 18 Nov 2011 18:25:55 -0500
>
>
>
>
>
>
>
>
> Fantastic - Thanks for the tips!
>
>
>
>> Subject: RE: Marco Variable Building of Code
>> Date: Fri, 18 Nov 2011 17:58:00 -0500
>> From: YBolotin@schools.nyc.gov
>> To: b_miner@LIVE.COM; SAS-L@LISTSERV.UGA.EDU
>>
>> You can try the below
>> But note that for most simple concatenation, you can use your select to
>> do it in the first place (the "separate by" clause can be *anything*)
>>
>>
>>
>>
>>
>> %macro plugMissing (inputTable);
>>
>> proc contents data=&inputTable out=fields nolist; /* nolist
>> prevents the annoying popup in the output window - put it back if you
>> need it to debug */
>> run;
>>
>> proc sql noprint; *noprint prevents that annoying output window
>> from popping up;
>> select NAME into : fields separated by ' ' from fields
>> where type=2;
>> quit; /*proc sql has to end in a QUIT, not a RUN */
>>
>> %let noVars = &sqlobs.; /* auto-created macro variable - saves
>> you from having to do that count(*) */
>>
>> %put noVars = &noVars.;
>>
>>
>> %let string = "Variables are: ";
>>
>> %do i = 1 %to &noVars.;
>> %let string = &string. %scan(&fields,&i,' '); /* macro
>> logic does not require concatenation operators to work */
>> %end;
>>
>> %put The value in string is: &string.;
>>
>> %mend;
>>
>>
>> data _z;
>> input a $ b $ c $ d $ e $ f $ g$ ;
>> datalines;
>> ;
>> run;
>>
>> %plugMissing(_z);
>>
>> -----Original Message-----
>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of b
>> miner
>> Sent: Friday, November 18, 2011 5:35 PM
>> To: SAS-L@LISTSERV.UGA.EDU
>> Subject: Marco Variable Building of Code
>>
>> Hi All,
>>
>> I am trying to build up a string which can be used for then a data step.
>> I am stuck at how to simply use a macro to dynamically build a string.
>>
>> Here is my simple example. When I run it, the value of string in the log
>> is:
>>
>> The value in string is "Variables are"||" "|| ACTIVE_CHK_IND||" "||
>> ANNUITY_IND||" "|| BANK_CARDHOLDER||" "||
>> BANK_CARD_NEW_ISSUE||" "|| BROKERAGE_IND||" "||
>> BROKERAGE_NON_PASSAGEWAY_IND||" "|| BROKERAGE_PASSAGEWAY_IND||" "|| etc
>> etc.
>>
>>
>> What is wrong? Thanks!
>>
>>
>>
>> %macro plugMissing (inputTable);
>>
>> proc contents data=&inputTable out=fields;
>> run;
>>
>> proc sql;
>> select NAME into : fields separated by ' ' from fields where type=2;
>> select count(*) into : noVars from fields where type=2;
>> run;
>>
>> %let string = "Variables are";
>>
>> %do i = 1 %to &noVars;
>> %let string= &string||" "|| %scan(&fields,&i,' ');
>> %end;
>>
>> %put The value in string is &string.;
>>
>>
>>
>> %mend;
>>
>>
>>
>
|