BookmarkSubscribeRSS Feed
KentUmeki94
Fluorite | Level 6

 

CATEGORYPRODUCTMEDIATOTAL DOLSWK 08/01/16 (B) DOLSWK 08/08/16 (B) DOLSWK 08/15/16 (B) DOLSWK 08/22/16 (B) DOLSWK 08/29/16 (B) DOLSWK 09/05/16 (B) DOLSWK 09/12/16 (B) DOLSWK 09/19/16 (B) DOLSWK 09/26/16 (B) DOLSWK 10/03/16 (B) DOLSWK 10/10/16 (B) DOLSWK 10/17/16 (B) DOLSWK 10/24/16 (B) DOLSWK 10/31/16 (B) DOLSWK 11/07/16 (B) DOLSWK 11/14/16 (B) DOLSWK 11/21/16 (B) DOLSWK 11/28/16 (B) DOLSWK 12/05/16 (B) DOLSWK 12/12/16 (B) DOLSWK 12/19/16 (B) DOLSWK 12/26/16 (B) DOLSWK 01/02/17 (B) DOLS
Exampleexampleexample505   45                  
Example 2example 2example 245   35                10  
example 3example 3example 330  20               10    

 

 

What I need:

 

CATEGORYPRODUCTMEDIATOTAL DOLSFirst Date spentLast Date spentWK 08/01/16 (B) DOLSWK 08/08/16 (B) DOLSWK 08/15/16 (B) DOLSWK 08/22/16 (B) DOLSWK 08/29/16 (B) DOLSWK 09/05/16 (B) DOLSWK 09/12/16 (B) DOLSWK 09/19/16 (B) DOLSWK 09/26/16 (B) DOLSWK 10/03/16 (B) DOLSWK 10/10/16 (B) DOLSWK 10/17/16 (B) DOLSWK 10/24/16 (B) DOLSWK 10/31/16 (B) DOLSWK 11/07/16 (B) DOLSWK 11/14/16 (B) DOLSWK 11/21/16 (B) DOLSWK 11/28/16 (B) DOLSWK 12/05/16 (B) DOLSWK 12/12/16 (B) DOLSWK 12/19/16 (B) DOLSWK 12/26/16 (B) DOLSWK 01/02/17 (B) DOLS
Exampleexampleexample5008/01/1608/29/165   45                  
Example 2example 2example 24508/22/1612/19/16   35                10  
example 3example 3example 33008/15/1612/05/16  20               10    

 

 

 

 

Here are the columns used above ^.

 

From the fifth column and on, there are dates for columns. Each row has a first and last date where dollars were spent. I am looking to create new columns called "Earliest Dollars Spent" and "Last Dollars Spent" for each row. 

 

What code can I write to do this? 

 

I am new to SAS and programming in general. It is hard to explain what I am doing but I can't show you pictures since it is private data. 


Thank you so much. 

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

This data layout is difficult to work with. Databases are not spreadsheets.

The data should be organised vertically, and the date should be in one column.

Now, to answer your question, something like this should work:

data HAVE;        
  length WK20160801 - WK20170102 8;
  array WK [*] WK20160801 -- WK20170102 ( . 3 . . 5 . ); * whatever the column names are;
run;

data WANT;
  set HAVE;
  length FIRST LAST $12;
  array WK  WK20160801 -- WK20170102; * whatever the column names are;
  do over WK;
    if WK then do;
      if FIRST=' ' then FIRST=vname(WK);
      LAST=vname(WK);
    end;
  end;
run;

 

KentUmeki94
Fluorite | Level 6

Hi, Thank you so much for the help. It has worked. It took me a little while to figure it out but you are a life saver. I know this is asking for a lot but is there anyway you could explain this part of the code to me and what is going on exactly?

 

 

data WANT;
  set HAVE;
  length FIRST LAST $12;
  array WK  WK20160801 -- WK20170102; * whatever the column names are;
  do over WK;
    if WK then do;
      if FIRST=' ' then FIRST=vname(WK);
      LAST=vname(WK);
    end;
  end;
run;

The first part of your code didn't seem to be needed since I imported the data from a excel file? 

KentUmeki94
Fluorite | Level 6
array WK  WK20160801 -- WK20170102;

the "--" part means all variables from WK20160801 to WK20170109?

 

Also the loop part. 

do over WK;
    if WK then do;
      if FIRST=' ' then FIRST=vname(WK);
      LAST=vname(WK);

 

"do over WK" means do the loop for the Array we created before right? I am unsure.

if WK then do;
      if FIRST=' ' then FIRST=vname(WK);
      LAST=vname(WK);

 

This whole part I am confused to be honest.

 

ChrisNZ
Tourmaline | Level 20
data WANT;
  set HAVE;
  length FIRST LAST $12;
  array WK  WK20160801 -- WK20170102;    %* put columns in an array, in data set order;
  do over WK;                            %* loop over array ;
    if WK then do;                       %* if the array element has a value then;
      if FIRST=' ' then FIRST=vname(WK); %* if it's the first value then save it;
      LAST=vname(WK);                    %* keep/overwrite the latest value ;
    end;
  end;
run;

Does this help?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 861 views
  • 2 likes
  • 2 in conversation