Arrays

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Arrays

My following code works without any errors and gives the correct results. However, I have a problem, which I have stated after the code.

%macro calculate2;
      %do i=1 %to 3;
       DATA na17_test_&i ;
            SET NA17 (keep=CQNO TIME_PERIOD MARKET &NA17_VAR WEIGHT);
            array na17_arr &NA17_VAR;
            do over na17_arr;   
                  IF na17_arr(&i) NE . THEN IF na17_arr(&i) > 1 THEN FLAG=1; ELSE FLAG=0;
                  QNUM="NA17_2_3_FAMILIARITY";
            end;
       run;
       PROC MEANS DATA=na17_test_&i NWAY NOPRINT;
            CLASS QNUM TIME_PERIOD MARKET;
            VAR FLAG;
            OUTPUT OUT=na17_test_&i._UW(DROP= _Smiley Happy SUM=CNT_UNWTD N=BASE_UNWTD MEAN=PER_UNWTD;
      RUN;
      PROC MEANS DATA=na17_test_&i NWAY NOPRINT;
            CLASS QNUM TIME_PERIOD MARKET;
            VAR FLAG;
            WEIGHT WEIGHT;
            OUTPUT OUT=na17_test_&i._W(DROP= _Smiley Happy SUM=CNT_WTD SUMWGT=BASE_WTD MEAN=PER_WTD;
      RUN;
      PROC SQL;
            CREATE TABLE na17_test_&i._MEANS2 AS
            SELECT A.TIME_PERIOD,A.QNUM, A.MARKET, &i AS NAMEPLATE, CNT_UNWTD, BASE_UNWTD, PER_UNWTD, CNT_WTD, BASE_WTD, PER_WTD FROM na17_test_&i._UW AS A
            JOIN na17_test_&i._W as B
            ON A.TIME_PERIOD=B.TIME_PERIOD AND A.QNUM=B.QNUM AND A.MARKET=B.MARKET 
            ORDER BY TIME_PERIOD, MARKET ;
      QUIT;
      PROC DELETE DATA=na17_test_&i na17_test_&i._UW na17_test_&i._W; RUN;

    

%end;
%MEND;


%CALCULATE2;

My question:

I have  %let NA17_VAR=NA17_1173 NA17_2041 NA17_2140;run; (about 100 values in reality); and instead of &i AS NAMEPLATE, I want Namplate = 1173 or 2041 etc. I probably need to use the substring function but I cannot get it to read 1 value at a time from the %let.

--------------------------------------------------------------------

2nd approach:

I have also tried this code:

proc sql NOPRINT;
   CREATE TABLE NA17_VARNAMES AS
    select name from dictionary.columns

   where libname = 'SQLDB'

   and memname= 't_fct_respondent' and NAME like 'NA17_%';     

quit;

and tried to use the %do %until loop but could not get what I needed.

Thank you in advance for any help.



Accepted Solutions
Solution
‎06-05-2012 06:32 PM
Super User
Posts: 11,343

Re: Arrays

Part of what you want, if I understand correctly is before the PROC SQL with Name plate you need to pull the variable name from the list.

%let vname = %scan(&NA17_var,&i);

then parse that name

%let val = %scan(&vname,2,'_'); /* the underscore here says to use the underscore as a word delimiter*/

Which can then be combined into a single

%let val = %scan(%scan(&NA17_var,&i),2,'_');

PROC SQL;

            CREATE TABLE na17_test_&i._MEANS2 AS

            SELECT A.TIME_PERIOD,A.QNUM, A.MARKET, &val AS NAMEPLATE,

View solution in original post


All Replies
Solution
‎06-05-2012 06:32 PM
Super User
Posts: 11,343

Re: Arrays

Part of what you want, if I understand correctly is before the PROC SQL with Name plate you need to pull the variable name from the list.

%let vname = %scan(&NA17_var,&i);

then parse that name

%let val = %scan(&vname,2,'_'); /* the underscore here says to use the underscore as a word delimiter*/

Which can then be combined into a single

%let val = %scan(%scan(&NA17_var,&i),2,'_');

PROC SQL;

            CREATE TABLE na17_test_&i._MEANS2 AS

            SELECT A.TIME_PERIOD,A.QNUM, A.MARKET, &val AS NAMEPLATE,

Contributor
Posts: 40

Re: Arrays

This worked....Thank you very much, ballardw !! I really appreciate your quick and correct response Smiley Happy...thanks, again!

Contributor
Posts: 66

Re: Arrays

Need some help on Arrays(Basic).

My question;

data aa;

input Name $  Marks1-Marks5;

datalines;

Jack 30 40 45 50 55

Tom 40 45 50 55 60

Don 45 34 36 78 50

;

I am trying to replace Marks of Jack by its first value(30).ie to get new data set bb such that

Jack 30 30 30 30 30

Tom 40 45 50 55 60

Don 45 34 36 78 50

I tried this

data bb; set aa;

array Mk(3,5) Marks1-Marks5  Marks6-Marks10 Marks11-Marks15;

do i=1 to 3;

do j=1 to 5;

Mk(1,j)=Mk(1,1);

end;

output;

end;

run;

Thanks in advance for your help.

PROC Star
Posts: 7,472

Re: Arrays

You really ought to start a new thread when you ask a question.

I can't test this at the moment, but there is no reason for you to use a 2 dimensional array.  Couldn't you just use something like?:

data bb;

  set aa;

  array Mk(*) Marks1-Marks5;

  If Name eq "Jack" then do _n_=2 to 5;

    Mk(_n_)=Mk(1);

  end;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 281 views
  • 0 likes
  • 4 in conversation