0

This is my current issue:

I have 53 variable headers in a SAS data set that need to be changed, for example:

Current_Week_0 TS   |    Current_Week_1 TS    |    Current_Week_2 TS  -- etc.

I need it to change such that Current_Week_# TS = Current_Week_# -- dropping the TS

Is there a way to automate this such as looping it like:

i = 0,53
Current_week_i TS = Current_Week_i  ?  

I just don't understand the proper syntax.

Edit: Thank you for editing my formats Sergiu, appreciate it! :)

Edit:

I used the following code, but I get the following error:
Missing numeric suffix on a numbered variable list (TS-Current_Week_53)

     DATA True_Start_8;
        SET  True_Start_7;
          ARRAY oldnames (53) Current_Week_1 TS-Current_Week_53 TS;
          ARRAY newnames (53) Current_Week_1-Current_Week_53;
          DO i = 1 TO 53;
               newnames(i) = oldnames(i) ;
          END;
     RUN;

@Joe EDIT

Here's what the data looks like before and after the "denorm" / transpose

BEFORE

Product     ID      CurrentWeek         Market  TS
X           75av2kz     Current_Week_0  Z       1
Y           7sav2kz     Current_Week_0  Z       1
X           752v2kz     Current_Week_1  Z       1
Y           255v2kz     Current_Week_1  Z       1


Product     ID      Market    Current_Week_0_TS    Current_Week_1_TS
X           75av2kz     Z         1                    0
Y           7sav2kz     Z         1                    1
X           752v2kz     Z         1                    1
Y           255v2kz     Z         1                    0
4

2 回答 2

0

另一种选择是在转置中执行此操作。您的示例数据要么与示例所需的输出不匹配,要么在逻辑中没有解释某些内容,但这只是简单的转置;如果你的 current_week_0/1 与下面的不同,这是合乎逻辑的,请解释原因。

data have;
format currentWeek $20.;
input Product  $ ID  $ CurrentWeek $ Market $ TS;
datalines;
X           75av2kz     Current_Week_0  Z       1
Y           7sav2kz     Current_Week_0  Z       1
X           752v2kz     Current_Week_1  Z       1
Y           255v2kz     Current_Week_1  Z       1
;;;;
run;

proc sort data=have;
by market id product;
run;
proc transpose data=have out=want;
by market id product ;
id currentWeek;
var TS;
run;
于 2013-11-14T18:59:47.950 回答
0

这不是太难。我假设这些是变量标签。

    proc sql;
    select cats('%relabel_nots(',name,')') into :relabellist separated by ' '
    from dictionary.columns
    where libname='WORK' and memname='True_Start_7'
    and name like '%TS';  *you may need to upper case the dataset name (memname) depending on your OS;
    quit;
%macro relabel_nots(name);
label &name.= substr(vlabel(&name.),1,length(vlabel(&name.))-3);
%mend relabel_nots;

data want;
set True_Start_7;
&relabellist.;
run;

基本上,PROC SQL 会抓取符合重新标记条件的不同名称,并生成一个带有所有重命名宏调用的大型宏变量。relabel_nots 宏生成新标签。如果变量名不包含 TS,您可能需要更改 PROC SQL 中 WHERE 背后的逻辑。

于 2013-11-14T16:28:46.893 回答