BookmarkSubscribeRSS Feed
KentUmeki94
Fluorite | Level 6

This is the code I have written so far but it does not work. There are a bunch of columns from different weeks named WK080116 to WK042720, these columns include how many dollars spent on that week. I am looking to create a new column called "spending_in_week_0" which is how many dollars spent on the week of its release. I have a column called Release_week which has the release of the product on that week. Release_week column has variables such as 080116. 

 

So I tried to make a code that will input the dollars spent on the release day to a column called Spending_in_week_0. But unfortunately, my code does not work. 

 

ProductWK080116WK080816WK081516WKetcWKetcWKetcWK042720Release_weekSpending_in_week_0 (I need this variable)
1$50      08011650
2 $20     080816$20
3  $70    081516$70
 

 

I need results that will create a new column called Spending in Week 0 (release week), that will give me the dollar amount spent on that week. 

 

DATA Assignment2_new_data_merged3;
set Assignment2_new_data_merged2;
array WK WK080116 -- WK042720;
do over WK;
if Release_week = substr(vname(WK),3,6) then Spending_in_week_0 = WK;
end;
run;

 

Please help me to create column Spending_in_week_0 please.

 

I would really appreciate it.

 

Thank you so much. 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Product WK080116 WK080816 WK081516 Release_week $;
infile datalines dlm=',' dsd;
datalines;
1,50,,,080116
2,,20,,080816
3,,,70,081516
;

data want;
   set have;
   Spending_in_week_0 = input(left(vvaluex(cats('WK', Release_week))), 8.);
run;
Tom
Super User Tom
Super User

Does your real data have only one value per observation like in your example? 

If so then just take the sum of all of the variables rather than trying to figure out how to access the specific one.

Spending_in_week_0 = sum( of wk:);

 

s_lassen
Meteorite | Level 14

The code you have shown seems to create the data you show as needed.

 

What is the problem?

 

If it is that you have more than one week's spending in some observations, you just need to output each row:

DATA Assignment2_new_data_merged3;
  set Assignment2_new_data_merged2;
  array WK WK080116 -- WK042720;
  do over WK;
  if Release_week = substr(vname(WK),3,6) then do;
    Spending_in_week_0 = WK;
    output;
    end;
end;
run;

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
  • 3 replies
  • 449 views
  • 0 likes
  • 4 in conversation